0

I'm attempting to parse a request for Twitch users playing a certain game . The results look like this:

{
    "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"}
}

I am attempting to parse it via:

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

The results I get look like this:

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')*

Ideally I'd like this to return:

user_Name, viewer_count, language,title

But I'm stuck at step one...

schwooples
  • 23
  • 5

1 Answers1

0

You're using too many for loops. This should work:

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