Checking if array is multidimensional or not?

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

提交回复
热议问题