How to check if variable is array?… or something array-like

后端 未结 6 1679
南方客
南方客 2020-12-04 22:46

I want to use a foreach loop with a variable, but this variable can be many different types, NULL for example.

So before foreach

6条回答
  •  孤城傲影
    2020-12-04 23:39

    You can check instance of Traversable with a simple function. This would work for all this of Iterator because Iterator extends Traversable

    function canLoop($mixed) {
        return is_array($mixed) || $mixed instanceof Traversable ? true : false;
    }
    

提交回复
热议问题