Jump to content

json_decode issue


7blake

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.