AndyUK
October 6, 2015, 6:30pm
1
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] => test@mailinator.com [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
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);
print_r($values);
http://php.net/manual/en/function.json-decode.php
1 Like
system
Closed
January 6, 2016, 4:45am
3
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.