How to traverse a JSON object in PHP?

Nixopax

New member
I have a JSON encoded object, and it's set up with integer keys for some of the internal objects.

$obj = '{"dimensions":{"5":{"name":"Quality"}}, "name":"Service"}';

Typically, doing a json_decode($obj) would allow you to obtain the various objects in PHP using standard object traversing.

$obj->name would return "Service"
But what if I wanted to get the name in the dimensions?
$obj->dimensions->5->name is invalid.
$obj->dimensions[5]->name is invalid.

Dimensions is an object, but since the key to the next object is an integer, there doesn't seem to be a method to access that object.
 
Back
Top