Christopher Lewis
Christopher Lewis

Reputation: 97

Json To PHP issues outputting an array

Im currently playing around with some php and JSON, i know abit of PHP pretty much nothing of JSON but ive managed to get about halfway where i need to be currently i have this section of code.

<?php
$json = file_get_contents('http://eu.battle.net/api/wow/character/Ragnaros/Smoosher?fields=progression');
$data = json_decode($json, true);

foreach ($data['progression']['raids'] as $raid) {
    echo "Raid: ", $raid['name'];
    echo " Normal: ", $raid['normal'];
    echo " Heroic: ", $raid['heroic'];
    echo " id: ", $raid['id'];
    echo " Boss's: ", $raid['bosses']['name'];
    ?>
    <p>
    <?php
};

foreach ($data['progression']['raids']['bosses'] as $boss) {
    echo " Boss Name: ", $boss['name'];
}

print_r($boss);
#echo $data->name;
?>

Now the first foreach works fine, no issues what that it pulls the data out into the page fine, however i noticed that seemed to be another (Array?) within the one i was out putting so first i tried

echo " Boss's: ", $raid['bosses']['name'];

Which outputs nothing but the word boss's, and nothing from within the array, so i thought maybe i need another foreach statement and start pulling that through. However this part

foreach ($data['progression']['raids']['bosses'] as $boss) {
    echo " Boss Name: ", $boss['name'];
}

Gives an error

Warning: Invalid argument supplied for foreach() in xxxxxx/xx/xxxxx/wow/test.php on line 22

And i cant see what im doing wrong :( any help would be great, the json im pulling from is

http://eu.battle.net/api/wow/character/Ragnaros/Smoosher?fields=progression

Upvotes: 0

Views: 167

Answers (3)

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

you should write like this for second foreach :- $data['progression']['raids'] hold array in it and ['bosses'] has an array element as member for $data['progression']['raids'].

foreach ($data['progression']['raids'] as $boss)
 foreach($boss['bosses'] as $boss1) 
 {
  echo " Boss Name: ", $boss1['name'];
  echo '<br>';
 }

Upvotes: 0

Jan Sverre
Jan Sverre

Reputation: 4713

$data['progression']['raids']

This variable is an array. You can use foreach at this array.

foreach($data['progression']['raids'] as $raid) {
    forach($raid['bosses'] as $boss) {
        //here is your boss
    }
}

Upvotes: 1

BenLanc
BenLanc

Reputation: 2434

Your problem here is that $data['progression']['raids']['bosses'] is not an array.

$data['progression']['raids'] is an array of arrays, and each of those arrays has a bosses key, so your data looks like this:

$data['progression']['raids'][0]['bosses'] - note the [0] in there, as opposed to just $data['progression']['raids']['bosses']

You should nest the loop on bosses within the previous loop, e.g.

<?php
$json = file_get_contents('http://eu.battle.net/api/wow/character/Ragnaros/Smoosher?fields=progression');
$data = json_decode($json, true);

foreach ($data['progression']['raids'] as $raid)
{
echo "Raid: ", $raid['name'];

echo " Normal: ", $raid['normal'];

echo " Heroic: ", $raid['heroic'];

echo " id: ", $raid['id'];

foreach ($raid['bosses'] as $boss)
{
  echo " Boss Name: ", $boss['name'];
}
?>
<p>
<?php
};

Upvotes: 2

Related Questions