1

What I use:

$raw = file_get_contents('url');
$raw = json_decode($raw,true);

foreach($raw['data'] as $spell){
  var_dump($spell);
}

What I get:

array(1) {
    ["image"]=> array(2){
        ["w"]=> int(48)
        ["h"]=> int(48)
    }
}

For now everything is fine.

But when I use a second loop (because of more than 1 keys & values) like this:

foreach ($raw['data'] as $spell){
    foreach ($spell['image'] as $image) {
        var_dump($image);
    }
}

I get:

int(48) int(48)

Nothing else.
I expected to get:

array(2){
    ["w"]=> int(48)
    ["h"]=> int(48)
}

What am i doing wrong?

ThaFlaxx
  • 71
  • 7
  • 2
    With the second foreach loop you go through the subArray `$raw["data"]["image"]` which only contain integer values and not arrays, so it prints those. Change the second foreach loop to: `foreach ($spell['image'] as $key => $image) echo "$key => $value \n";` Then you see your key. If you want your expected output you just want: `var_dump($raw["data"]["image"]);` – Rizier123 Jun 12 '16 at 23:33
  • Had to read your answer three times, but now I got it and it works. Thanks so much :) – ThaFlaxx Jun 12 '16 at 23:44
  • But, how can I get them by the `['w']` and `['h']`, I need to print them multiple times out. – ThaFlaxx Jun 13 '16 at 00:04
  • If you want to access them directly see: http://stackoverflow.com/q/30680938/3933332 – Rizier123 Jun 13 '16 at 00:16
  • Got it! I just needed one loop with `foreach ($raw['data'] as $spell)` and `print $spell['image']['x'];` in it. Thanks – ThaFlaxx Jun 13 '16 at 00:30

1 Answers1

0

With the second foreach loop you go through the subArray $raw["data"]["image"] which only contain integer values and not arrays, so it prints those. Change the second foreach loop to: foreach ($spell['image'] as $key => $image) echo "$key => $value \n"; Then you see your key.

I used

foreach ($raw['data'] as $spell){
    print $spell['image']['x'];
}

Thanks to Rizier123 for the answer!

ThaFlaxx
  • 71
  • 7