php - JSON array foreach loop with "where" attribute -
i have json array directly api , 1 piece of looks this:
{ "type": "champion", "version": "4.4.3", "data": { "aatrox": { "id": "aatrox", "key": "266", "name": "aatrox", "title": "the darkin blade", "stats": { "armor": 14.0, "armorperlevel": 3.8, "attackdamage": 55.0, "attackdamageperlevel": 3.2, "attackrange": 150.0, "attackspeedoffset": -0.04, "attackspeedperlevel": 3.0, "crit": 0.0, "critperlevel": 0.0, "hp": 395.0, "hpperlevel": 85.0, "hpregen": 5.75, "hpregenperlevel": 0.5, "movespeed": 345.0, "mp": 30.0, "mpperlevel": 45.0, "mpregen": 0.0, "mpregenperlevel": 0.0, "spellblock": 30.0, "spellblockperlevel": 1.25 } }, and repeats every other champion. used curl turn php array, looks this:
$url="api_url_blah"; $ch = curl_init(); curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_url,$url); $result=curl_exec($ch); $array = json_decode($result, true); from there, made foreach loop list champions , "armor" stat, armor won't display champion name does:
$i = 1; foreach($array['data'] $champs) { echo $champs['id']. "<br>"; foreach($champs['stats'] $stats) { echo $stats['armor']; } $i++; } as said, champion name comes second foreach loop returning nothing. also, wondering convenient way make (after works) can call 1 champion's stats based on php variable , not 118 of them @ 1 time.
this var_dump() of array:
array(3) { ["type"]=> string(8) "champion" ["version"]=> string(5) "4.4.3" ["data"]=> array(118) { ["aatrox"]=> array(5) { ["id"]=> string(6) "aatrox" ["key"]=> string(3) "266" ["name"]=> string(6) "aatrox" ["title"]=> string(16) "the darkin blade" ["stats"]=> array(20) { ["armor"]=> float(14) ["armorperlevel"]=> float(3.8) ["attackdamage"]=> float(55) ["attackdamageperlevel"]=> float(3.2) ["attackrange"]=> float(150) ["attackspeedoffset"]=> float(-0.04) ["attackspeedperlevel"]=> float(3) ["crit"]=> float(0) ["critperlevel"]=> float(0) ["hp"]=> float(395) ["hpperlevel"]=> float(85) ["hpregen"]=> float(5.75) ["hpregenperlevel"]=> float(0.5) ["movespeed"]=> float(345) ["mp"]=> float(30) ["mpperlevel"]=> float(45) ["mpregen"]=> float(0) ["mpregenperlevel"]=> float(0) ["spellblock"]=> float(30) ["spellblockperlevel"]=> float(1.25) } }
you don’t need second foreach loop. following code should work:
$i = 1; foreach($array['data'] $champs){ echo $champs['id'] . "<br/>" . $champs['stats']['armor'] . "<br/>"; $i++; }
Comments
Post a Comment