PHP warning: invalid argument supplied for foreach()

Fix for PHP Warning – Invalid argument supplied for foreach() in wordpress plugin php, symfony, codeigniter, laravel and yii2 or other PHP programming languages.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

While working on a WordPress, PHP, Laravel, yii2, codeigniter, symfony or any other application, I got a PHP warning.

How to Solve PHP warning: invalid argument supplied for foreach()?

Warning: Invalid argument supplied for foreach() in …

I can simply hide warnings in PHP server side languages. But I like to know advance all about such simple fix issue for “invalid argument supplied for foreach() in” matters in deep.

Actually this error code is coming from a foreach loop. I just try to print the php variable like as a var_dump() values in an array using this foreach loop.

foreach ($products as $product) {
 // ...
}

In the PHP manual, it says that

The PHP Loop construct supported an easy way to PHP Loop foreach iterate over arrays. foreach works only on arrays and objects, and will issue an error code when you try to use it on a variable with a different data type or an uninitialized variable. and php get a “warning: invalid argument supplied for foreach() in”.

This is the basic cause behind this warning. I am passing something to foreach loop that is not an array.

To resolved this warning, I have two simple options at here.

Solution 1

I can check the type of that PHP variable & if it is an array, then only pass it to the foreach loop.

if (is_array($products)) {
 foreach ($products as $product) {
 // ...
 }
}

Now I ensure that I have an array for foreach loop.

Solution 2

I can also resolved this warning by cast that PHP variable to an array in the loop. This is a lot cleaner, requires less typing as well as only required single edit on a single line.

foreach ((array) $products as $product) {
 // ...
 }

If I cast any value to an array, I will get an array whatever value it was.

Fixing the “invalid argument supplied for foreach()” PHP error

Consider the source code PHP snippet below:

<?php 
function fetchWebCMSList () {
  // a list/array, a boolean value like as FALSE is varible returned.
  return false;
}

// Get the array from a function call.
$websiteInfo = fetchWebCMSList();

//Loop through the $websiteInfo array.
foreach($websiteInfo as $arrayItem)
{
  //Do something.
}
?>

This Best 100% working solution is display in the source code below: invalid argument supplied for foreach() in

<?php 

function fetchWebCMSList () {
  // a list/array, a boolean value like as FALSE is varible returned.
  return false;
}

// Get the array from a function call.
$websiteInfo = fetchWebCMSList();

// Check if $websiteInfo is indeed an array or an object.
if (is_array($websiteInfo) || is_object($websiteInfo))
{
  // If yes, then foreach() will iterate over it in PHP.
  foreach($websiteInfo as $arrayItem){
      //Do something.
  }
}
else // If $websiteInfo was not an array, then this block is executed. 
{
  echo "Sorry, Unfortunately, generated an error some occured.";
}
?>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about warning: invalid argument supplied for foreach() wordpress.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Laravel carbon convert String To Date
  2.   PHP AJAX Live Search Box Autocomplete Using MySQL Database
  3.   Everything You Need to Know About Date format in PHP
  4.   Removing # hash from URL in AngularJS
  5.   Ajax Login System with jQuery PHP and MySQLi
  6.   jQuery Multiple Class Selectors Match Examples
  7.   Date filtering and formatting in VueJS with Example
  8.   How to Make a Redirect to page in PHP?
  9.   Laravel Validation Unique Soft Delete Example
  10.   Check if row exists using Laravel