2015-02-24 200 views
0
$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 />'; 
} 

並非所有名稱都在「脫機」部分中回顯,並且有些名稱正在兩次(在線和脫機狀態下)回顯。陣列不會刪除所有元素

enter image description here

回答

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