【问题标题】:PHP, error Undefined offset [closed]PHP,错误未定义的偏移量[关闭]
【发布时间】:2013-09-14 14:47:21
【问题描述】:

我有一个脚本,其中列出了一些 Twitch.tv 流并显示它们是离线还是在线。 我正在尝试将它集成到我的一个页面中,但我遇到了几个未定义的偏移错误。

这是脚本:

<html>
<head>
<title>Streamlist</title>
</head>
<body>
<?php

$members = array("painuser","ZombieGrub","Nathanias","Youbetterknowme","ncl_tv");

// 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=";
$userGrab .= implode(",", $members);


//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
$channels = array();

for ($i = 0; $i < count($members); $i++) {
 $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;
}
}

?>
<hr>
<?php
//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 $ii => $value2) {
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>';
echo '<a href="http://www.twitch.tv/'.$value.'"> <strong>'.$value.'</strong></a>';
echo '&nbsp<img src="/images/offline.png"> <strong> Status :</strong> Offline! </br>';

}


?>
</body>
</html>

在此之后,我使用此代码来显示流:

//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>';
}

现在,错误。 我得到了这个:

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

有人可以帮忙吗?有没有办法解决这个问题? 我不是很擅长PHP,我不知道该怎么做。

编辑: 第二个错误...

zombiegrub   Status: Online! 
Viewers:  588
nathanias   Status: Online! 
Viewers:  178
youbetterknowme   Status: Online! 
Viewers:  80
painuser   Status: Online! 
Viewers:  47

Notice: Undefined offset: 4 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 34

Notice: Undefined offset: 4 in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 37

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83

Notice: Undefined variable: online in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 83
Status: Online! 

Notice: Undefined variable: viewers in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 85
Viewers:  

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86

Notice: Undefined variable: value in C:\xampp\htdocs\wp-content\plugins\twitchlist\twitchlist.php on line 86
Status : Offline! 

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\themes\sahifa\sidebar.php on line 75

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wp-content\themes\sahifa\sidebar.php on line 84

【问题讨论】:

  • 你能不能打开报错所示的文件,让我们确切知道第42行和第45行是哪一行,这样对你的帮助会更容易。
  • 第 42 行:$title = $json_array['$i']['channel']['channel_url'];
  • 第 45 行:$viewer = $json_array['$i'] ['stream_count'];
  • 尝试 print_r 或 var_dump $json_array 的内容。使用它来查看该变量的内容。 echo '
    ',print_r($json_array),'
    ';
    就像我在解码 json_string 的行下编写的代码一样,看看它有什么。它可能是空的,因此您在数组中寻找未定义的键。
  • 您确定其他服务器甚至正在向您发送 JSON 字符串吗?这是我希望您添加的上述行的目的。只是为了确保你可能什么都得不到。如果不知道 twitch.tv 上的其他服务器发送给您的数据格式,真的很难提供帮助。

标签: php undefined offset


【解决方案1】:

这是不正确的:

 $json_array['$i']....

PHP 将单引号内的字符串视为字符串字面量 - 在这种情况下,您要查找的数组值是 $i,而不是值。如果要在字符串中插入值,则应使用双引号:

 $json_array["$i"]....

但由于您只是使用变量的值,因此根本不需要引号:

 $json_array[$i]....

编辑:

你也可以试试这个。如果你应该传递一个逗号分隔值的数组,你的代码会在开头给你一个额外的逗号:

$members = array("maegis","androide456","avonmexicola","iksf");

$usergrab .= implode(",", $members);

编辑第二个:

返回的 JSON 是一个数组;所以要遍历它,你需要一个简单的循环:

for ($i = 0; $i < count($members); $i++) {
     $title = $json_array[$i]['channel']['channel_url'];
     ....

【讨论】:

  • 好的,已编辑。但是,无论我是否使用双引号,我仍然得到未定义的偏移量。
  • 在这种情况下,您将不得不按照 Touch 的建议进行操作,var_dump($json_array) 这样您就可以看到格式。你能把它编辑成你的问题吗?
  • 现在也在我的问题中编辑。 var_dump($json_array) 返回格式 NULL。
  • 我添加了另一个建议。您需要确保您的 file_get_contents 调用正确返回数据。
  • 它实际上无法正常工作。我现在设置了这个 $members = array("painuser","ZombieGrub","Nathanias","Youbetterknowme","ncl_tv");然而它仍然返回 array("maegis","androide456","avonmexicola","iksf")
猜你喜欢
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多