Home > Backend Development > PHP Problem > PHP determines the number of dimensions of an array

PHP determines the number of dimensions of an array

藏色散人
Release: 2023-03-02 21:16:01
Original
2941 people have browsed it

php method to determine how many dimensions an array is: first obtain the array that needs to be determined; then use the custom "aL" method to determine how many dimensions the array is; finally use the "max($al)" method Just get the dimensions of the array.

PHP determines the number of dimensions of an array


##PHP Gets the number of dimensions of an array

Recommended: "

PHP Tutorial"

// 判断数组是几维数组
$data = array(); // 是你要判断的数组
$al = array(0);
function aL($data,&$al,$level=0){
    if(is_array( $data )){
        $level++;
        $al[] = $level;
        foreach($data as $v){
            aL($v,$al,$level);
        }
    }
}
aL($data,$al);
$num_level = max($al);
// $num_level 就是你要获取的数组的维度
Copy after login

The above is the detailed content of PHP determines the number of dimensions of an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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