如何在PHP中获得一个庞大的数组

时间:2014-04-28 05:29:23

标签: php arrays json twitch

我正在重新编码我的Stream Refresher,但是如何获得这个庞大的数组列表呢?如何将它们输出到变量中?我真的不知道如何构建我的代码来获取这些数组。

这是数组:

  

https://api.twitch.tv/kraken/streams?channels=iTzYoHero,WackyMackie,stingsempai,gouki2304,onex64,TSM_WildTurtle,swaglul,TSM_Bjergsen,Meteos,yoonie,Trick2g,ImaBlondeGoat,Quendal43,mattzillla,Melorian,gurlsrbadatvidyagaems,Wingsofdeath,Scarra,PhreakStream,HarukaShinigami4,Deathlylove,amberplays,presidentjacky,DarkFaith93,riotgames,TSM_TheOddOne,flosd,TSM_Xpecial,riotgamesbrazil,RAIDERRRLoL,Chrono_Frost,Sojirox,TSM_Dyrus,secretagentthai,wheresswaldo,Bacon_Donut,Soaryn,dotastarladder_en,TwitchPlaysPokemon,SHYLIZZLE,tx9_vanilla,djinn4583,die4frags,cyb3r_tech,DeAxiom,Too_malicious,MegaCarlos50,SynergyDrink,Entosian,HibubTv,Crs_saintvicious,HaiL9,arenix320,Nightblue3,Bischulol,BrandoPB,Trollbaltz,zzang18nom,hir0shisan,Skunkodor,screamingpikachu,GarenaEsports,GarenaTW,wraithxzz,SuNolol,RiotGamesLatino,ICanFindDiamond,naxious_magnus,xenadiscord,cathiesousa,Tharnambular,wuppo1,mkay299,WordlessHope,Killboy520,support_heroftw,enlightenwolf,TolkinNA,Winteruo,TheOddLeeSin,Synren,robjeah,2DaMaxx,WearyStreams,ido1994,THRILL_505%20,Dirtsensei

这是我的尝试(我不知道我说实实在在做什么)

<?php
header('Content-Type: text/html; charset=UTF-8');
$con = mysqli_connect("localhost", "0", "0", '0');
$result = mysqli_query($con, "SELECT * FROM mybb0_streams");
$array_ids;
while ($row = mysqli_fetch_array($result)) {
  $array_ids[] = $row['channel'];
}
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams?channels=' . implode(',',$array_ids)), true);
foreach($json_array as $user){
    if ($json_array['stream'] != NULL) {
        // turn them into variables to prevent outside SQL injection
        $displayname = mysqli_real_escape_string($con, $json_array[0]['channel']['name']);
        $title       = mysqli_real_escape_string($con, $json_array[0]['title']);
        $game        = mysqli_real_escape_string($con, $json_array[0]['channel']['meta_game']);
        $viewers     = mysqli_real_escape_string($con, $json_array['stream']['viewers']);
        $preview     = mysqli_real_escape_string($con, $json_array['stream']['preview']['medium']);
        $followers   = mysqli_real_escape_string($con, $json_array['stream']['channel']['followers']);
  echo 'Display Name: $displayname | Title: $title | Game: $game | Viewers: $viewers | Followers: $followers <br />';
    }

}
echo "http://api.justin.tv/api/stream/list.json?channel=" . implode(',',$array_ids);
?>

1 个答案:

答案 0 :(得分:0)

就像您使用implode()使用逗号,连接所有频道,就像使用,打破字符串一样> explode()

$channels = explode(',',$_GET['channels']);
foreach($channels as $val){
        echo $val;
}

就像我之前提到的那样,你应该考虑通过其他方式传递GET参数,因为URL的字符限制。