Saturday, 14 September 2013

PHP, error Undefined offset

PHP, error Undefined offset

I have a script that lists a few Twitch.tv and shows if they are offline
or online. I'm trying to integrate it in one of my pages, but I'm getting
several Undefined offset errors.
This is the script:
$members = array("maegis","androide456","avonmexicola","iksf");
// This variable becomes one long url with the channel names stringed up
behind it
// This url then fetches a json file from twitch with all the selected
channels information
$userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
//I use this array to compare with the members array. All users in this
arrat are substracted from the members array and hence are //"offline"
$checkedOnline = array ();
foreach($members as $i =>$value){
$userGrab .= ",";
$userGrab .= $value;
}
unset($value);
//grabs the channel data from twitch.tv streams
$json_file = file_get_contents($userGrab, 0, null, null);
$json_array = json_decode($json_file, true);
//get's member names from stream url's and checks for online members
foreach($members as $i =>$value){
$title = $json_array['$i']['channel']['channel_url'];
$array = explode('/', $title);
$member = end($array);
$viewer = $json_array['$i'] ['stream_count'];
onlinecheck($member, $viewer);
$checkedOnline[] = signin($member);
}
unset($value);
unset($i);
//checks if player streams are online
function onlinecheck($online, $viewers)
{
//If the variable online is not equal to null, there is a good change this
person is currently streaming
if ($online != null)
{
echo '<a href="http://www.twitch.tv/'.$online.'">
<strong>'.$online.'</strong></a>';
echo '&nbsp <img src="/images/online.png"><strong> Status:</strong>
Online! </br>';
echo '<img src="/images/viewers.png"><strong>Viewers:</strong> &nbsp'
.$viewers.'</br>';
}
}
//This funcion add's online channel names to the checked online array
function signin($person){
if($person != null){
return $person;
}
else{
return null;
}
}
After this, I'm using this code to display the streams:
//This part list all the people currently offline. Here the array with
online users is compared with the total users.
//online users are then removed from the total users array.
foreach ($members as $i => $value1) {
foreach($checkedOnline as $ii => $value2){
if($value1 == $value2){
unset($members[$i]);
}
}
}
//print a nice list with people that can't currently be bothered with
streaming their games
foreach ($members as $i => $value) {
echo '<a href="http://www.twitch.tv/'.$value.'">
<strong>'.$value.'</strong></a>';
echo '&nbsp<img src="/images/offline.png"> <strong> Status :</strong>
Offline! </br>';
}
Now, the errors. I'm getting this:
Notice: Undefined offset: 0 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42
Notice: Undefined offset: 0 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 45
Notice: Undefined offset: 1 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42
Notice: Undefined offset: 1 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 45
Notice: Undefined offset: 2 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42
Notice: Undefined offset: 2 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 45
Notice: Undefined offset: 3 in
C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 42
Warning: Invalid argument supplied for foreach() in
C:\xampp\htdocs\wp-content\themes\fesport\sidebar.php on line 75
Warning: Invalid argument supplied for foreach() in
C:\xampp\htdocs\wp-content\themes\fesport\sidebar.php on line 84
Can someone help? Is there a way to solve this? I'm not that good at PHP
and I don't know what to do.

No comments:

Post a Comment