0

一直在通过stackoverflow寻找这个,所以我确实尝试过纠正这个问题。不知道我在这里做错了什么。有什么建议么?

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

1 回答 1

5

你看起来不够认真。PHP手册清楚地说明了以下参数json_decode

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

通过传递第二个参数,因为true你告诉它给你一个关联数组(不是对象),所以你需要像这样访问它:

$result['users'][0]['render_info']['profile_url'];
于 2013-03-08T13:35:13.183 回答