Home > php教程 > php手册 > body text

PHP中如何判断数组是一维、二维或几维的?

WBOY
Release: 2018-09-27 17:57:05
Original
1228 people have browsed it

这篇文章主要介绍了PHP实现判断数组是一维、二维或几维的方法,涉及php递归操作及数组相关判定技巧,需要的朋友可以参考下

本文实例讲述了PHP实现判断数组是一维、二维或几维的方法。分享给大家供大家参考,具体如下:

这里使用的自定义函数,可以判断数组是一维的,还是二维的,或是几维的数组:

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

验证过可以使用:

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

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template