Checking if array is multidimensional or not?

后端 未结 25 3205
醉话见心
醉话见心 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:21

    In my case. I stuck in vary strange condition.
    1st case = array("data"=> "name");
    2nd case = array("data"=> array("name"=>"username","fname"=>"fname"));
    But if data has array instead of value then sizeof() or count() function not work for this condition. Then i create custom function to check.
    If first index of array have value then it return "only value"
    But if index have array instead of value then it return "has array"
    I use this way

     function is_multi($a) {
            foreach ($a as $v) {
              if (is_array($v)) 
              {
                return "has array";
                break;
              }
              break;
            }
            return 'only value';
        }
    

    Special thanks to Vinko Vrsalovic

    0 讨论(0)
  • 2020-11-28 02:23

    The native print_r function returns a human-readable string. Just count the "Array" instances.

    try...

    substr_count(print_r([...array...], true), 'Array') > 1;
    
    $a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
    $b = array(1 => 'a',2 => 'b');
    $c = array(1 => 'a',2 => 'b','foo' => array(1,array(2)));
    $d = array(array());
    $e = array(1, array());
    $f = array(array(), array());
    $g = array("hello", "hi" => "hi there");
    $h[] = $g;
    
    var_dump(substr_count(print_r($a, true), 'Array') > 1);
    ...
    
    //a: bool(true)
    //b: bool(false)
    //c: bool(true)
    //d: bool(true)
    //e: bool(true)
    //f: bool(true)
    //g: bool(false)
    //h: bool(true)
    

    On my box, "is_multi took 0.83681297302246 seconds in 500000 times"

    Courtesy: Ruach HaKodesh

    0 讨论(0)
  • 2020-11-28 02:24

    I think you will find that this function is the simplest, most efficient, and fastest way.

    function isMultiArray($a){
        foreach($a as $v) if(is_array($v)) return TRUE;
        return FALSE;
    }
    

    You can test it like this:

    $a = array(1 => 'a',2 => 'b',3 => array(1,2,3));
    $b = array(1 => 'a',2 => 'b');
    
    echo isMultiArray($a) ? 'is multi':'is not multi';
    echo '<br />';
    echo isMultiArray($b) ? 'is multi':'is not multi';
    
    0 讨论(0)
  • 2020-11-28 02:24
    $is_multi_array = array_reduce(array_keys($arr), function ($carry, $key) use ($arr) { return $carry && is_array($arr[$key]); }, true);
    

    Here is a nice one liner. It iterates over every key to check if the value at that key is an array. This will ensure true

    0 讨论(0)
  • 2020-11-28 02:24
    function isMultiArray(array $value)
    {
        return is_array(reset($value));
    }
    
    0 讨论(0)
  • 2020-11-28 02:25

    This function will return int number of array dimensions (stolen from here).

    function countdim($array)
    {
       if (is_array(reset($array))) 
         $return = countdim(reset($array)) + 1;
       else
         $return = 1;
    
       return $return;
    }
    
    0 讨论(0)
提交回复
热议问题