I have a JSON array directly from an API and one piece of it looks like 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 then it simply repeats this for every other champion. I used cURL to turn that into a PHP array, which looks like 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, I made a foreach loop to list all the champions and their "armor" stat, however the armor won't display but the champion name does:
$i = 1;
foreach($array['data'] as $champs)
{
echo $champs['id']. "<br>";
foreach($champs['stats'] as $stats) {
echo $stats['armor'];
}
$i++;
}
As I said, the champion name comes up but the second foreach loop is returning nothing. Also, I was wondering what would be the most convenient way to make it so (after this works) I can call just one champion's stats based on a PHP variable and not all 118 of them at one time.
This is the var_dump() of the 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) } }
var_dump()
.... – zerkms Mar 25 '14 at 2:14var_dump()
what exactly? – user1895377 Mar 25 '14 at 2:16stats
is not a sub array ofdata
– Dagon Mar 25 '14 at 2:17var_dump
what actual data you have. You're in doubts about$champs['stats']
? Check it. Confused what's in$stats
? Check it. Programming is about facts, not about belief. – zerkms Mar 25 '14 at 2:26