Home > Article > Backend Development > How does php determine how many dimensions an array has?
This article mainly introduces the method of judging whether an array is one-dimensional, two-dimensional or several-dimensional in PHP, involving phprecursive operations and array-related judgment skills, friends in need You can refer to the following
The example of this article describes the method of PHP to determine whether an array is one-dimensional, two-dimensional or several-dimensional. Share it with everyone for your reference, the details are as follows: Thecustom function used here can determine whether the array is one-dimensional, two-dimensional, or several-dimensional:
function getmaxdim($vDim) { if(!is_array($vDim)) return 0; else { $max1 = 0; foreach($vDim as $item1) { $t1 = $this->getmaxdim($item1); if( $t1 > $max1) $max1 = $t1; } return $max1 + 1; } }It has been verified that it can be used:
//测试: $arr=array('yiyi'=>1212,'haha'=>array('heihei'=>array(array("a")),"b")); echo getmaxdim($arr); //结果: 4The following code will also determine how many dimensions the array is. Friends who need it can refer to it
<?php /** * 返回数组的维度 * @param [type] $arr [description] * @return [type] [description] */ function arrayLevel($arr){ $al = array(0); function aL($arr,&$al,$level=0){ if(is_array($arr)){ $level++; $al[] = $level; foreach($arr as $v){ aL($v,$al,$level); } } } aL($arr,$al); return max($al); } ?>
The above is the detailed content of How does php determine how many dimensions an array has?. For more information, please follow other related articles on the PHP Chinese website!