JSON使用PHP获取选定的值

Bul*_*fen 0 php json

一直在寻找关于这个的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
    ?>
Run Code Online (Sandbox Code Playgroud)

Col*_*n M 5

你看起来不够努力.PHP手册清楚地说明了以下参数json_decode:

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

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

$result['users'][0]['render_info']['profile_url'];
Run Code Online (Sandbox Code Playgroud)