Checking if array is multidimensional or not?

后端 未结 25 3206
醉话见心
醉话见心 2020-11-28 01:41
  1. What is the most efficient way to check if an array is a flat array of primitive values or if it is a multidimensional array?
相关标签:
25条回答
  • 2020-11-28 02:48

    You can also do a simple check like this:

    $array = array('yo'=>'dream', 'mydear'=> array('anotherYo'=>'dream'));
    $array1 = array('yo'=>'dream', 'mydear'=> 'not_array');
    
    function is_multi_dimensional($array){
        $flag = 0;
        while(list($k,$value)=each($array)){
            if(is_array($value))
                $flag = 1;
        }
        return $flag;
    }
    echo is_multi_dimensional($array); // returns 1
    echo is_multi_dimensional($array1); // returns 0
    
    0 讨论(0)
提交回复
热议问题