0

Been looking thru stackoverflow about this so I did try to get this correct. Not sure what I'm doing wrong here. Any suggestions?

 <?php
    $string = '{
    "status": "success",
    "th_size": "small",
    "users": [
        {
            "user_id": 159826,
            "username": "WillBeUsed",
            "online": true,
            "is_away": false,
            "access": "public",
            "render_info": {
                "profile_url": "/person1",
                "profile_image": "11e5a496f13366a0c4ce000403aa862",
                "language": "english",
                "chat": "ready"
            },
            "new": false,
            "back": false
        }
    ],
    "online_count": 1
}';
    $result = json_decode($string, true);

//echo $result['status']; // This works
echo $result->users[0]->render_info->profile_url; // This don't work
    ?>
2
  • You should have done var_dump($result) to check whether the result is an object or an assosciative array. You would have saved a lot of time... Commented Mar 8, 2013 at 13:56
  • 1
    Thx Girish. I'll keep it in mind next time. :D Have a nice weekend. Commented Mar 8, 2013 at 14:14

1 Answer 1

5

You didn't look hard enough. The PHP manual clearly states the parameters for json_decode:

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] );

By passing the second parameter as true you are telling it to give you back an associative array (not an object), so you need to access it like this:

$result['users'][0]['render_info']['profile_url'];
Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh.. Right, see it now. Thank you for taking time to reply. Works perfect. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.