Checking if array is multidimensional or not?

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

提交回复
热议问题