foreach() argument must be of type array|object¶
Description¶
foreach() works on arrays or objects.
For the arrays, all the elements of the array are read.
For the objects, it is either the public properties, read like an array; or the object has the Iterator interface: then, there is a list of specific methods to implements, which control how the object is traversed; the object may be a generator, which is a method that uses the yield
keyword; finally, the object may be a PHP native iterator, or a child of them.
Then, any other type is forbidden to be used with foreach(). This means all scalars elements, including string, integer and null.
Example¶
<?php
$source = null;
foreach($source as $x) { }
?>
Solutions¶
Check that the source of the foreach can be used with is_iterable().
See Also¶
In previous PHP versions, this error message used to be Invalid argument supplied for foreach().