I'm using a basic LAMP server for my website and looking to rewrite a script I have that's linked with Twitch's API. The issue that I'm having is trying to find, for lack of a better word, the opposite of the foreach.
For example I have an array of names that are sent with a URL to the Twitch servers, and if one of the names are currently streaming "streams" will have lots of values, and if they aren't currently streaming "streams" will return null.
This works wonderful for checking people and showing they are online, but I also need it to display the ones who are offline and I can't figure out how to do that. Something like a foreachelse or something like that. This is the code I have below. Thanks.
<?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>';
}
?>
I don't see the problem, if you just use array_diff, you can filter out the usernames.
$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);
Output (at time of writing):
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