0

我正在尝试解析 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')*

理想情况下,我希望这个返回:

user_Name, viewer_count, language,title

但是我卡在第一步...

4

1 回答 1

0

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

<?php
        foreach($results['data'] as $data){
            echo ($data['user_name']."<br/>");
        }
于 2019-06-10T02:39:54.463 回答