Getting stdClass Object values

I have converted some JSON using json_decode, I’ve then used print_r to display the array, as shown below:

stdClass Object ( [orders] => Array ( [0] => stdClass Object ( [id] => 1429118021 [email] => [closed_at] => [created_at] => 2015-10-06T18:31:45+01:00 [updated_at] => 2015-10-06T18:31:45+01:00 [number] => 2

I am trying to display the ID number, and I was expecting to use:

echo $json['orders'][0]['id']

But that would have been too simple :stuck_out_tongue:

As you discovered. json_decode returns objects instead of an array by default. I think pretty much everyone has been surprised by this. Just set the second parameter to true to get an array.

$values = json_decode($jsonString,true);

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.