PHP method to determine whether an array is one-dimensional, two-dimensional, or multi-dimensional_PHP tutorial

WBOY
Release: 2016-07-13 17:00:13
Original
1332 people have browsed it

Sometimes our array is dynamically generated, and we don’t know how many dimensions the array is. Now I will introduce to you the PHP method to determine whether the array is one-dimensional, two-dimensional, or multi-dimensional. Friends who need to know more can enter refer to.


Column 1

The code is as follows
 代码如下 复制代码

/**

* 返回数组的维度

* @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);

}

?>

Copy code

代码如下 复制代码

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

/** * Return the dimensions of the array * @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); } ?> Example 2 You can determine whether it is one-dimensional, two-dimensional, or an array of several dimensions:
The code is as follows Copy code
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;                 } } Verified and ready to use. //Test $arr=array('yiyi'=>1212,'haha'=>array('heihei'=>array(array("a")),"b")); echo getmaxdim($arr); //Result 4 http://www.bkjia.com/PHPjc/631274.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631274.htmlTechArticleSometimes our array is dynamically generated, and we don’t know how many dimensions the array has. Let me tell you A classmate introduced the PHP method to determine whether an array is one-dimensional, two-dimensional, or multi-dimensional. If necessary...
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