Invalid argument supplied for foreach()

后端 未结 19 1357
花落未央
花落未央 2020-11-21 06:32

It often happens to me to handle data that can be either an array or a null variable and to feed some foreach with these data.

$values = get_val         


        
19条回答
  •  再見小時候
    2020-11-21 06:34

    There seems also to be a relation to the environment:

    I had that "invalid argument supplied foreach()" error only in the dev environment, but not in prod (I am working on the server, not localhost).

    Despite the error a var_dump indicated that the array was well there (in both cases app and dev).

    The if (is_array($array)) around the foreach ($array as $subarray) solved the problem.

    Sorry, that I cannot explain the cause, but as it took me a while to figure a solution I thought of better sharing this as an observation.

提交回复
热议问题