Home > Backend Development > PHP Tutorial > PHP implements a method to determine whether an array is one-dimensional, two-dimensional or several-dimensional

PHP implements a method to determine whether an array is one-dimensional, two-dimensional or several-dimensional

墨辰丷
Release: 2023-03-28 06:34:02
Original
1806 people have browsed it

This article mainly introduces the method of PHP to determine whether an array is one-dimensional, two-dimensional or several-dimensional, involving PHP recursive operations and array-related determination skills. Friends in need can refer to the following

Examples of this article PHP implements a method 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


Verified and can be used:


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



The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP Two-dimensionalHow to sort an associative array according to one of the fields

php array implements a detailed explanation of the method of merging the same key values ​​​​according to a certain key value to generate a new two-dimensional array

PHP implements the method of deleting the same elements and duplicate values ​​in the array in two-dimensional

The above is the detailed content of PHP implements a method to determine whether an array is one-dimensional, two-dimensional or several-dimensional. 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