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? Quote Link to comment https://forums.phpfreaks.com/topic/295891-json_decode-issue/ Share on other sites More sharing options...
Solution Barand Posted April 27, 2015 Solution 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 1 Quote 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 1 Quote 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 (edited) 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 Edited April 27, 2015 by 7blake Quote Link to comment https://forums.phpfreaks.com/topic/295891-json_decode-issue/#findComment-1510112 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.