Jump to content

json_decode issue


Go to solution Solved by Barand,

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

  • Solution

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
  • Like 1
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

Edited by 7blake
Link to comment
https://forums.phpfreaks.com/topic/295891-json_decode-issue/#findComment-1510112
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.