0

我正在为我的网站使用基本的 LAMP 服务器,并希望重写我拥有的与 Twitch 的 API 链接的脚本。我遇到的问题是试图找到与 foreach 相反的词,因为没有更好的词。

例如,我有一组名称,它们通过 URL 发送到 Twitch 服务器,如果其中一个名称当前正在流式传输,“流”将有很多值,如果它们当前没有流式传输,“流”将返回无效的。

这对于检查人员并显示他们在线非常有用,但我还需要它来显示离线的人,我不知道该怎么做。类似于 foreachelse 之类的东西。这是我下面的代码。谢谢。

<?php
$username = array("zeromi", "aerosgw", "krylen","tshirt_aion","snooky_aion","vashiro","papa456","vinley_aion","hanfkrokette","wtfast_siel","neckofthewood","paraproc","aionnae","uhiwi","mufflermankr","valorium","knighterrantry","soulune","relizex3","vinlockz","trevyn201","tiger529","xkegi","logsnsticks","meowform","uzuk3","kalzard01","squall_m","suyji","headpcgamer","sariett_siel");
$callAPI = implode(",",$username);
$data = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true);


foreach ($data['streams'] as $streams){
$name       = $streams['channel']['name']; 
echo $name.'<br>';
}

?>
4

1 回答 1

1

我看不出问题,如果你只使用array_diff,你可以过滤掉用户名。

$online = array();
foreach ($data['streams'] as $streams)
    $online[] = $streams['channel']['name'];

$offline = array_diff($username, $online);

echo 'Online users: ' . implode(', ', $online) . "\n<br>";
echo 'Offline users: '. implode(', ', $offline);

输出(在撰写本文时):

Online users: sariett_siel, mufflermankr 
Offline users: zeromi, aerosgw, krylen, tshirt_aion, snooky_aion, vashiro, papa456, vinley_aion, hanfkrokette, wtfast_siel, neckofthewood, paraproc, aionnae, uhiwi, valorium, knighterrantry, soulune, relizex3, vinlockz, trevyn201, tiger529, xkegi, logsnsticks, meowform, uzuk3, kalzard01, squall_m, suyji, headpcgamer
于 2014-08-11T06:53:28.160 回答