Undefined offset in repeat loop in PHP

1

I'm using the twitch.tv API. I'm getting all the lives from "fifa17" and listing all the channel names that are doing this live with this game, the URL has 100 results limit ie 100 names, I made a for to list all the names, for shows 89 names and the rest of the missing names it the error Undefined offset: 89

<?php

$api2 = file_get_contents("https://api.twitch.tv/kraken/search/streams?client_id=g5ynk8n0llmefg9m70ruyg36bbt6si&query=fifa17&limit=100");

$defuse = json_decode($api2);

if($defuse == null){
    echo "erro";
}else{

    $total_lives=$defuse->_total;

    echo "Toltal de Lives:  ".$total_lives."</br>";

    for($i=0;$i<=99;$i++)
        echo $defuse->streams[$i]->channel->name."</br>";
}

?>

    
asked by anonymous 27.04.2017 / 03:50

1 answer

2

The number 100 indicates to the webservice the limit and not the exact quantity it should return. So you should not expect that 100 results will always return.

To resolve, use the% loop of% loop

for($i=0;$i<=99;$i++)
    echo $defuse->streams[$i]->channel->name."</br>";

It would look like this:

foreach($defuse->streams as $v)
    echo $v->channel->name."</br>";
    
27.04.2017 / 03:58