Home > Article > Backend Development > How to determine the dimension of an array in php Example code
The dimension of an array is an element in an array. When expressed by an array subscript, several numbers are needed to uniquely determine this element. This array has several dimensions.
For example, a number determines an element: a[7] is one-dimensional
Two numbers determine an element: b[5][9] is two-dimensional
Three numbers: c [6][8][1] is three-dimensional
…………
n numbers are n-dimensions
You can think of the dimensions of an array as “the number of layers of an array within an array”
For example, the array {1, 5, 9, 0} is a one-dimensional array. To find the number 9, you only need to find the third number.
{ {1,2}, {5,5}, {2,4}, {9,0} } is a two-dimensional array. It contains two levels of arrays.
To find the number 9, you need to indicate which element 9 is in the outer array and which element it is in the inner array. So it needs to be marked with two numbers: the first element in the fourth element of the array is 9.
This article is a detailed analysis and introduction to the code for judging the dimension of the php array. For reference, the code is as follows:
<?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); } $arr = array( '0'=>'0', ); echo arrayLevel($arr); ?>
The above is the detailed content of How to determine the dimension of an array in php Example code. For more information, please follow other related articles on the PHP Chinese website!