Home > Backend Development > PHP Problem > How to determine the dimensions of an array in php

How to determine the dimensions of an array in php

藏色散人
Release: 2023-03-03 16:52:01
Original
3328 people have browsed it

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. .

How to determine the dimensions of an array in php

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

It can be used after verification:

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

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!

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