Warning: Invalid argument supplied for foreach()

A common error which occurs with foreach loops is the standard “Invalid argument supplied for foreach()” whch gets thrown up as a warning.

This can easily be overcome by wrapping the foreach loop in a conditional if statement which checks to see if the argument supplied to the loop is an array or not.

Below is an example of how to correct the problem:

if (in_array($arrItem)) {
  foreach($arrItem as $arrItemi) {
    // use $arrItemi;
  }
}
Tags:

1 thought on “Warning: Invalid argument supplied for foreach()”

Leave a Reply

Your email address will not be published.