Checking if array is multidimensional or not?

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

    You can simply execute this:

    if (count($myarray) !== count($myarray, COUNT_RECURSIVE)) return true;
    else return false;
    

    If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

    If it's the same, means there are no sublevels anywhere. Easy and fast!

提交回复
热议问题