sushibrain
sushibrain

Reputation: 2780

array returns null outside foreach but has values inside foreach

I have an array named slideIcons. This array is filled from inside a foreach loop like this:

$slideIcons = [];
foreach ($data[$key]["icons"] as $icon) {
    $slideIcons[] = $icon["preview_url"];
}

However, when I try to do this:

die(var_dump(($slideIcons)

from outside of the foreach loop (after the loop), the printed result is that it's an empty array. Which is weird, because if I run the code like this:

$slideIcons = [];
foreach ($data[$key]["icons"] as $icon) {
    $slideIcons[] = $icon["preview_url"];
    die(var_dump($slideIcons));
}

It prints:

array(1) {
    [0]=>
    string(54) "https://d30y9cdsu7xlg0.cloudfront.net/png/2300-200.png"
}

So it seems to be that the $slideIcons variable is reset somehow, however I don't see any way of that being possible.

For troubleshooting, I changed the name of the variable to be 100% certain that it isn't being overwritten, but that didn't change the outcome. I also tried to replace

$slideIcons[] = $icon["preview_url"];

for:

array_push($slideIcons, $icon["preview_url"]);

but that didn't change the outcome either. So is it possible that, for some wild reason, the variable is being reset to its base value, or am I missing something here?

Thanks.

Edit: Some more information, the $key variable is passed onto this function by its parent, which contains a foreach loop. However, this shouldn't matter, since the $slideIcons variable is saved into the database before it gets reset by it's parent foreach. For easy I included the full function:

 private function createSlides($key, $answer, $data)
 {
    $slideIcons = [];

    foreach ($data[$key]["icons"] as $icon) {
        $slideIcons[] = $icon["preview_url"];
    }

    $keywords = $data[$key]['keywords'];
    $image_keywords = $data[$key]['image_keywords'];
    $images = $data[$key]['images'];
    $content = $this->addContent($keywords, $images);

    $this->presentation->slides()->create([
        'presentation_id' => $this->presentation->id,
        'pitch_answer_id' => $answer->id,
        'order' => $key,
        'keywords' => $keywords,
        'image_keywords' => $image_keywords,
        'images' => $images,
        'icons' => $slideIcons,
        'content' => $content
    ]);
}

And when I var_dump($data[$key]["icons"]) (before the foreach loop) this is the result:

array(4) {
  [0]=>
  array(23) {
    ["attribution"]=>
    string(44) "people by Roman J. Sokolov from Noun Project"
    ["attribution_preview_url"]=>
    string(62) "https://d30y9cdsu7xlg0.cloudfront.net/attribution/2300-600.png"
    ["collections"]=>
    array(0) {
    }
    ["date_uploaded"]=>
    string(10) "2012-04-26"
    ["id"]=>
    string(4) "2300"
    ["is_active"]=>
    string(1) "1"
    ["is_explicit"]=>
    string(1) "0"
    ["license_description"]=>
    string(28) "creative-commons-attribution"
    ["nounji_free"]=>
    string(1) "0"
    ["permalink"]=>
    string(17) "/term/people/2300"
    ["preview_url"]=>
    string(54) "https://d30y9cdsu7xlg0.cloudfront.net/png/2300-200.png"
    ["preview_url_42"]=>
    string(53) "https://d30y9cdsu7xlg0.cloudfront.net/png/2300-42.png"
    ["preview_url_84"]=>
    string(53) "https://d30y9cdsu7xlg0.cloudfront.net/png/2300-84.png"
  }
}

(There are more array elements that just that one, but they all have the same structure.)

Upvotes: 0

Views: 1863

Answers (1)

Guillermo Phillips
Guillermo Phillips

Reputation: 2216

I can't claim any solid reasoning behind this, just programmer's intuition, try:

$icons = $data[$key]["icons"];
foreach ($icons as $icon) {
    $slideIcons[] = $icon["preview_url"];
}

Upvotes: 1

Related Questions