Invalid argument supplied for foreach() with correct supplications!

Asked

Viewed 70 times

0

Guys I have the following resource 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 exit I am using 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 making a mistake on this one: foreach ($value as $temp) { I have no idea why, someone can see the error?

1 answer

2


It is that not all $value that goes to your second foreach return an Array, you must make a check if it is an array before running a foreach and give the value you want if it is not an Array.

  • really, forgot is_array($value) thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.