JSON using PHP to get selected values

535

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
    ?>
971

Answer

Solution:

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

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

By passing the second parameter astrue 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'];

People are also looking for solutions to the problem: php - showing fifth post in wordpress

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.