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

How to determine the dimensions of an array in php

藏色散人
藏色散人Original
2020-08-01 10:31:343275browse

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;
  }
}

It can be used after verification:

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

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!

Statement:
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