Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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) } }
share|improve this question
1  
var_dump() .... –  zerkms Mar 25 '14 at 2:14
    
@zerkms var_dump() what exactly? –  user1895377 Mar 25 '14 at 2:16
    
stats is not a sub array of data –  Dagon Mar 25 '14 at 2:17
    
@user1895377: of every variable, if you don't have any particular suspect –  zerkms Mar 25 '14 at 2:17
1  
@user1895377: oh my god. STOP guessing, check with var_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

1 Answer 1

up vote 1 down vote accepted

You don’t need a second foreach loop. The following code should work:

$i = 1;
foreach($array['data'] as $champs){
echo $champs['id'] . "<br/>" . $champs['stats']['armor'] . "<br/>";
$i++;
}
share|improve this answer
    
whats the $i for ? –  andrew Mar 25 '14 at 2:29
    
@andrew I don’t know. Perhaps the author of the question needs it to count the number of iterations. But then he/she should begin with $i = 0. –  Sharanya Dutta Mar 25 '14 at 2:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.