Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a PHP object property that is a number [duplicate]

This should work:

$object->content->{'5'}


Another possibility is to use the 2nd parameter to json_decode:

$obj = json_decode(str, true);

You get an array instead of a PHP object, which you can then index as usual:

$obj['content'][5]

JSON encode, and then decode your object passing true as the second param in the decode function. This will return an associative array.

$array = json_decode(json_encode($object), true);

Now you can use your new array

echo $array['content']['5'];

Using $object->content->{'5'} will not work if the object was created by casting an array to an object.

A more detailed description can be found here: https://stackoverflow.com/a/10333200/58795