Get a PHP object property that is a number

I'm trying to get a property from JSON data decoded into a PHP object. It's just a YouTube data API request that returns a video object that has a content object liks so;

[content] => stdClass Object
                    [5] =>
                    [1] => rtsp://
                    [6] => rtsp://



Throws "unexpected T_DNUMBER" - which makes perfect sense. But how do I get the value of a property that is a number?

I'm sure I should know this. Thanks in advance.


This should work:


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:


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:

Another aproach is casting the object to array.

$array = (array) $object;

Need Your Help

Git Diff - How do I revert selected lines or chunks

git diff git-gui

In Git GUI I can select parts of a diff and stage just those lines or chunks. How would I do the opposite, as in roll back changed lines in a file. Usually these are accidental white space changes ...

Is there a Python library to handle OWL?

python semantic-web owl

It could be something as big as Jena or something smaller. Do you know anything?