解析 Twitch API 响应 PHP

Parsing Twitch API response PHP

我正在尝试解析针对玩特定游戏的 Twitch 用户的请求 .结果如下所示:

{
    "data":
    [{
        "id":"id here",
        "user_id":"uid here",
        "user_name":"name here",
        "game_id":"gameid here",
        "community_ids":[IDs here],
        "type":"live",
        "title":"awesome title here",
        "viewer_count":10,000,000,
        "started_at":"time here",
        "language":"en",
        "thumbnail_url":"url here",
        "tag_ids":[look at all these tags!]
    },{
        "id":"id here",
        "user_id":"uid here",
        "user_name":"name here",
        "game_id":"gameid here",
        "community_ids":[IDs here],
        "type":"live",
        "title":"awesome title here",
        "viewer_count":10,000,000,
        "started_at":"time here",
        "language":"en",
        "thumbnail_url":"url here",
        "tag_ids":[look at all these tags!]
        etc. etc.
    }],
    "pagination":{"cursor":"whatever this does"}
}

我正在尝试通过以下方式解析它:

$results = json_decode($query, TRUE);
foreach($results as $data){
    foreach($data as $users){
        echo ($users['user_name']."<br/>");
    }
}

我得到的结果如下所示:

name 1
...
name n

Warning: Illegal string offset 'user_name'
*first letter of pagination thing here (in this example it would be the 'w' in 'whatever')*

理想情况下,我想 return:

user_Name, viewer_count, language,title

但我卡在了第一步...

您使用了太多 for 循环。这应该有效:

<?php
        foreach($results['data'] as $data){
            echo ($data['user_name']."<br/>");
        }