Home > Backend Development > PHP Tutorial > How does php determine how many dimensions an array has?

How does php determine how many dimensions an array has?

怪我咯
Release: 2023-03-13 11:38:01
Original
4258 people have browsed it

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:

The

custom 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;
  }
}
Copy after login

It has been verified that it can be used:

//测试:
$arr=array('yiyi'=>1212,'haha'=>array('heihei'=>array(array("a")),"b"));
echo getmaxdim($arr);
//结果: 4
Copy after login

The 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);
}
?>
Copy after login

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!

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