Array不会删除所有元素

时间:2015-02-24 17:52:01

标签: php arrays json

$channels = array('imaqtpies','imsoff','zzero71tv', 'kaptenen', 'onlySinged', 'nightblue3') ;
$nr = 0;


$callAPI = implode(",",$channels);
$online = 'online.png';     
$offline = 'offline.png';   
$json = file_get_contents('https://api.twitch.tv/kraken/streams?channel=' .     $callAPI);
$dataArray = json_decode($json, true);

foreach($dataArray['streams'] as $mydata){
    echo $mydata['channel']['name'] . ' is online';
    echo '<br /><hr />';
    unset($channels[$nr]);
    $nr++;
}

$newChannels = array_values($channels);;


foreach($newChannels as $channel) {
    echo $channel . ' is offline';
    echo '<br /><hr />';
}

并非所有名称都在&#34;离线&#34;部分和一些名称被回复两次(在线和离线)。

enter image description here

1 个答案:

答案 0 :(得分:3)

$mydata['channel']['name']$nr未对齐。您已取消设置第一个 x 频道,但我不知道为什么抽搐应按照您定义频道的顺序返回。

您需要以下内容:

$online_channels = array();
foreach($dataArray['streams'] as $stream){
    $online_channels[] = $stream["channel"]["name"];
}
$offline_channels = array_diff($channels, $online_channels);

然后打印$online_channels$offline_channels