Cycle99
Cycle99

Reputation: 182

PHP 7.2.7 Warning: Invalid argument supplied for foreach()

Getting a warning message with PHP 7.2.7, but with PHP 7.2.11 it works fine.

Warning: Invalid argument supplied for foreach() 
foreach ($result->data as $posty) {  

Any idea? It was working fine with PHP 5.2 too.

Upvotes: 1

Views: 10755

Answers (1)

Andrey Parsadanian
Andrey Parsadanian

Reputation: 103

This thing is changed in php7. Please check if you pass valid array or object to the foreach loop.

The error you are experiencing means that the php interpreter can't cycle through items of your $result->data.

I usually check structures before passing them to foreach like this:

if ($result->data && (gettype($result->data)=='array' || gettype($result->data )=='object')) {
 foreach ($result->data as $posty) {
  ...
 }
}

Upvotes: 4

Related Questions