0

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

3 Answers 3

2

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
};
Sign up to request clarification or add additional context in comments.

2 Comments

I see thats great, should of tried the loop within the loop first before asking but that does help a ton thanks.
yea was stuck on a timer had to wait 5 mins before i could accept it :(
1
$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
    }
}

Comments

0

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>';
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.