Home > Article > Backend Development > How to determine the dimensions of an array in php
php method to determine how many dimensions an array is: first create a PHP sample file; then define a getmaxdim method; then determine how many dimensions the array is through foreach loop traversal; finally output the judgment result through echo. .
Recommended: "PHP Video Tutorial"
The custom function used here can determine whether the array is one-dimensional , or two-dimensional, or several-dimensional array:
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 can be used after verification:
//测试: $arr=array('yiyi'=>1212,'haha'=>array('heihei'=>array(array("a")),"b")); echo getmaxdim($arr); //结果: 4
The above is the detailed content of How to determine the dimensions of an array in php. For more information, please follow other related articles on the PHP Chinese website!