How to check if a multidimensional array is empty or not?

前端 未结 6 1272
[愿得一人]
[愿得一人] 2021-02-01 05:20

Basically, I have a multidimensional array, and I need to check whether or not it is simply empty, or not.

I currently have an if statement trying to do thi

6条回答
  •  佛祖请我去吃肉
    2021-02-01 06:02

    If you don't know the structure of the multidimensional array

    public function isEmpty(array $array): bool
    {
        $empty = true;
    
        array_walk_recursive($array, function ($leaf) use (&$empty) {
            if ($leaf === [] || $leaf === '') {
                return;
            }
    
            $empty = false;
        });
    
        return $empty;
    }
    

    Just keep in mind all leaf nodes will be parsed.

提交回复
热议问题