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

Solution 1:

This should work:

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

Solution 2:

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]

Solution 3:

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