Home > Backend Development > PHP Tutorial > How to determine the dimension of an array in php Example code

How to determine the dimension of an array in php Example code

怪我咯
Release: 2023-03-13 15:34:01
Original
2629 people have browsed it

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( 
    &#39;0&#39;=>&#39;0&#39;, 
); 

echo arrayLevel($arr); 
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template