无法从数组中获取最后一个关注者的用户名

时间:2014-07-09 23:39:13

标签: php json twitch

尝试在网上找到的一个片段的帮助下获取最新的Twitch-follower的名字。但是我收到一条错误消息,我无法理解

$json_array2 = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/'.strtolower($channelName).'/follows?limit=1'), true);

$latestFollower = $json_array2['follows']['user']['name'];

在我的浏览器中输入网址我得到了这个,所以我想 - > user->名称应该是正确的但我猜不是:(

    {
    "follows": [
        {
            "created_at": "2014-07-09T23:30:59Z",
            "_links": {
                "self": "https://api.twitch.tv/kraken/users/username17376/follows/channels/sodapoppin"
            },
            "user": {
                "_id": 65845277,
                "name": "username17376",
                "created_at": "2014-07-08T03:59:16Z",
                "updated_at": "2014-07-08T04:14:14Z",
                "_links": {
                    "self": "https://api.twitch.tv/kraken/users/username17376"
                },
                "display_name": "Username17376",
                "logo": null,
                "bio": null,
                "type": "user"
            }
        }
    ],
    "_total": 339627,
    "_links": {
        "self": "https://api.twitch.tv/kraken/channels/sodapoppin/follows?direction=DESC&limit=1&offset=0",
        "next": "https://api.twitch.tv/kraken/channels/sodapoppin/follows?direction=DESC&limit=1&offset=1"
    }
}

1 个答案:

答案 0 :(得分:0)

应该是

$json_array2['follows'][0]['user']['name'];

基于您的JSON

{
    "follows": [
        { 

这表明follows是一个数组。数组中的第一个对象(索引0)是一个具有属性" user"

的对象