Invalid argument supplied for foreach () with correct replacements!

0

I have the following feature in php :

array(3) {
  ["name"]=> string(12) "module teste"
  ["status"]=> string(1) "1"
  ["banner_image"]=>
  array(1) {
    [1]=>
    array(1) {
      [0]=>
      array(7) {
        ["title"]=> string(12) "module teste"
        ["link"]=> string(12) "module teste"
        ["width"]=> string(3) "200"
        ["height"]=> string(3) "200"
        ["image"]=> string(20) "catalog/logotipo.png"
        ["image_hover"]=> string(26) "catalog/banner1366x532.png"
        ["sort_order"]=> string(1) "1"
      }
    }
  }
}

And to organize this output I'm hovering these foreach's :

foreach ($banner_images as $key => $value) {
    foreach ($value as $temp) {
        foreach ($temp as $banners) {
            if (is_file(DIR_IMAGE . $banners['image'])) {
                $image = $banners['image'];
                $thumb = $banners['image'];
            } else {
                $image = '';
                $thumb = 'no_image.png';
            }

            if (is_file(DIR_IMAGE . $banners['image_hover'])) {
                $image_hover = $banners['image_hover'];
                $thumb_hover = $banners['image_hover'];
            } else {
                $image_hover = '';
                $thumb_hover = 'no_image.png';
            }

            $data['banner_images'][$key][] = array(                 
                'title'      => $banners['title'],
                'link'       => $banners['link'],
                'image'      => $image,
                'image_hover'=> $image_hover,
                'width'      => $banners['width'],
                'height'     => $banners['height'],
                'thumb'      => $this->model_tool_image->resize($thumb, 100, 100),
                'thumb_hover'=> $this->model_tool_image->resize($thumb_hover, 100, 100),
                'sort_order' => $banners['sort_order']
            );
        }
    }
}

But I'm having an error on this page: foreach ($value as $temp) { I have no idea why, can anyone see the error?

    
asked by anonymous 21.09.2017 / 17:14

1 answer

2

It's because not all the $ values that go to your second foreach return an Array, you should check if it's an array before executing a foreach and give it the value you want if it is not an Array.

    
21.09.2017 / 18:12