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

php判断数组是一维、二维、还是多维方法

WBOY
Release: 2016-06-13 10:04:17
Original
892 people have browsed 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);

}

?>

例二

可以判断是一维的,还是二维的,或是几维的数组:

代码如下 复制代码

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

验证过可以使用.

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

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
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!