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; } }
Code language: PHP (php)
Tags:
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
CyrIng
9 years ago

Hi,

I’m rather using is_array since in_array require two arguments