I’m working on fetching stream data and I need help accessing different values from multiple objects in a JSON response. Here’s my current setup:
$apiResponse = file_get_contents('https://api.example.com/search/live?game=fps&count=2');
$decodedData = json_decode($apiResponse, true);
I can get the first stream’s data fine:
echo $decodedData['results'][0]['title']; // Works - shows first stream
echo $decodedData['results'][0]['watchers']; // Shows 245
But I’m confused about how to get the second stream’s viewer count. The API returns data like this:
{
"total": 1200,
"results": [
{
"id": 12345,
"title": "First Stream",
"watchers": 245,
"quality": "720p"
},
{
"id": 67890,
"title": "Second Stream",
"watchers": 189,
"quality": "1080p"
}
]
}
I want to display both viewer counts but I’m not sure how to access the second one. I tried using the same array index but that only gives me the first result. How do I get both values like this:
echo $decodedData['results'][0]['watchers']; // Shows 245
echo $decodedData['results'][1]['watchers']; // Should show 189
Thanks for any help!
I encountered a similar issue recently with streaming APIs. It’s possible that the game you’re using for testing lacks multiple active streams at the moment. Try switching to a more popular game category temporarily to verify that your code can handle multiple results.
Additionally, consider checking for any potential rate limits, as exceeding them might result in incomplete data. To investigate, log the raw response before using json_decode by adding error_log($apiResponse). This will allow you to see precisely what the server is sending back.
Once you’ve confirmed that both streams are being returned, accessing [1]['watchers'] should work perfectly for retrieving the second viewer count.
Double-check your API call first - those endpoints can be weird about exact counts. But yeah, $decodedData['results'][1]['watchers'] should work if the data’s there. I’d wrap it in an if statement though: if(isset($decodedData['results'][1])) { echo $decodedData['results'][1]['watchers']; } This prevents errors when there’s only one stream returned.
Your syntax looks fine. The problem’s probably that you’re not getting two results back from the API. I’ve hit this with streaming APIs before - the count parameter doesn’t guarantee you’ll actually get that many results, especially if there aren’t enough active streams for that game.
Add some debugging to see what you’re actually getting. Try var_dump($decodedData['results']) or count($decodedData['results']) to check if both objects are there. Also check the ‘total’ value to make sure the API endpoint’s working right.
If you’re only getting one result, that second array index won’t exist and you’ll get an undefined index error. Once you confirm you’ve got both objects, $decodedData['results'][1]['watchers'] should work fine.
Your code for accessing the second stream’s viewer count looks right. I’ve hit this same issue before - it’s usually about API response timing. Most streaming APIs cache results for several minutes, so if you’re testing with the same parameters repeatedly, you’re probably getting cached data that only had one stream when it was first pulled.
Try adding a timestamp parameter to your API call or switch up the game parameter to force a fresh response. Also, streaming APIs sometimes return fewer results during off-peak hours when there just aren’t enough active streams matching what you’re looking for.
I’d check the array length first with something like if(count($decodedData['results']) > 1) before accessing the second element to avoid errors. Your $decodedData['results'][1]['watchers'] syntax is spot on once you confirm the data’s actually there.