7blake Posted April 27, 2015 Share Posted April 27, 2015 I'm grabbing an object with objects that is converted to JSON, then decoded in PHP newObject = {"firstObject":{"Color" : "red"}, "secondObject":{"Color":"blue"}}; anyhow, I can get this to PHP fine, and access it with the foreach loops. But I can't access "firstObject" or "secondObject". $someArray = json_decode($_POST['containerPositions'], true); foreach($someArray as $value){ foreach ($value as $secondKey){ echo getClass($value) . " " . $secondKey;} } Could someone tell me what is the correct method for accessing them? Link to comment https://forums.phpfreaks.com/topic/295891-json_decode-issue/ Share on other sites More sharing options...
Barand Posted April 27, 2015 Share Posted April 27, 2015 Do you mean you can't access the keys of the outer array? $someArray = json_decode($newObject, true); foreach($someArray as $obj=>$value){ foreach ($value as $secondKey){ echo $obj . " " . $secondKey . "<br>"; } } gives firstObject red secondObject blue Link to comment https://forums.phpfreaks.com/topic/295891-json_decode-issue/#findComment-1510058 Share on other sites More sharing options...
nik_jain Posted April 27, 2015 Share Posted April 27, 2015 $someArray = json_decode($_POST['containerPositions'], true); The true parameter causes an associative array to be returned. If you need an ofject set it false http://php.net/manual/en/function.json-decode.php Link to comment https://forums.phpfreaks.com/topic/295891-json_decode-issue/#findComment-1510062 Share on other sites More sharing options...
7blake Posted April 27, 2015 Author Share Posted April 27, 2015 Yes thankyou both @Brand - IThanks for clarifying and supplying the correct way. I appreciate it. I was using the foreach on the Value, not the Key from the outer object. Bah. Thanks! @nik_jain - Thankyou for that bit of information. That's a pretty crucial fork in the road. Cheers Link to comment https://forums.phpfreaks.com/topic/295891-json_decode-issue/#findComment-1510112 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.