1

I can't convert PHP object to an array and send it to JavaScript file as an object. Here is what I'm doing: Getting user object from database - it looks like this:

Array
(
    [0] => AppBundle\AdminBundle\Entity\AdminUser Object
    (
        [id:protected] => 1
        [name:protected] => Kamil
        [surname:protected] => Stangelj
        [birthdate:protected] => DateTime Object
            (
                [date] => 1996-01-28 00:00:00.000000
                [timezone_type] => 3
                [timezone] => Europe/Berlin
            )

        [email:protected] => kamiltomice@gmail.com
        [login:protected] => Sztanga
        [password:protected] => $2y$15$qyD9tbeIcXqRKTQeUJEiLehWo16XKy447NVWU7q/VyvYSD0k61f6C
        [image:protected] => resources_admin/images/upload/users/1510092373jDuHxJkOnGp1RNxTOy0OBez nazwy-1.png
        [role:protected] => ["1","2","3","4","5","6","7","8","9","10"]
        [lock_date:protected] => DateTime Object
            (
                [date] => 2017-12-17 15:20:08.000000
                [timezone_type] => 3
                [timezone] => Europe/Berlin
            )

        [is_active:protected] => 1
        [is_non_locked:protected] => 1
        [lock_info:protected] => 
    )
)

Another step I was doing is to convert it via object_to_array function (from that example that I get from this Stack answer - this answer also didn't help me). This is the function from the link above:

public function objectToArray($data)
{
    if(is_array($data) || is_object($data))
    {
        $result = array();            
        foreach($data as $key => $value) {
            $result[$key] = UsefulController::objectToArray($value);
        }            
        return $result;
    }        
    return $data;
}

And there's a problem because that function returns me an empty array:

Array
(
    [0] => Array
    (
    )
)

If someone dind't understood my problem. I want to convert PHP object that i pointed first to an array and pass it back to my JavaScript file store it as an object.

In the very very big nutshell:

INPUT DATA: PHP Object

OUTPUT DATA: JavaScript Object

3 Answers3

0

there is a php function for thisjson_encode()

you can check documentation http://php.net/manual/en/function.json-encode.php

0

I think AdminUser entity should have gatter and setter methods. You have to manually create php associative array for this. Like

array('id' => $user-> getId());

Then using json_decode you can convert it into json string.

0

The only way to slove it was to create an array and encode it then send..

$result['id'] = $user->getId();
$result['name'] = $user->getName();
$result['surname'] = $user->getSurname();
$result['birthdate'] = $user->getBirthdate()->format('d-m-Y');
$result['email'] = $user->getEmail();
$result['login'] = $user->getLogin();
$result['image'] = $user->getImage();
$result['role'] = $user->getRole();
$result['is_active'] = $user->getIsActive();
$result['is_non_locked'] = $user->getIsNonLocked();
$result['lockdate'] = $lockdate;
$result['lockinfo'] = $user->getLockinfo();