Undefined offset in repetition loop in PHP

Asked

Viewed 117 times

1

I’m using the Twitch.tv. I’m getting all the Lives by "fifa17" and listing all the channel names that are doing this live with this game, in the URL has the limit of 100 results ie 100 names, I made a for to list all the names, the for shows 89 names and rest of the missing names it gives 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>";
}

?>

inserir a descrição da imagem aqui

1 answer

2


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

To resolve, use the repeat loop foreach()

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

Would look like this:

foreach($defuse->streams as $v)
    echo $v->channel->name."</br>";
  • Our partner thank you so much for the help worked out here and also thank you for the explanation that God bless you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.