Home >Backend Development >PHP Tutorial >php中如何快速确定多维数组的深度?

php中如何快速确定多维数组的深度?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:30:381357browse

例如有一个多维数组:

<pre class="brush:php;toolbar:false;">array( 
        array( 
            array(1,3,4), 
            array( 
                array( 
                    1,2,3 
                ) 
            ) 
        ), 
        array( 
            array(1,2), 
            array(1) 
        ) 
    )

这个数组的深度就是5,那么如何快速的确定一个数组深度.

解决方案1:

使用SPL中的Iterator,在RecursiveIteratorIterator 类中有个getDepth方法,获得深度

解决方案2:

<?php 
function wei($arr){ 
    global $wei,$num; 
    if(is_array($arr))$num++; 
    for($i=0;$i<count($arr);$i++){ 
                if(is_array($arr[$i])){ 
                        wei($arr[$i]); 
                        $element = true; 
                } 
    } 
        if(true != $element){ 
                $wei[] = $num; 
                $num = 1; 
        } 
} 
$test_arr[] = 1; 
$test_arr[] = array(array(1,array(1,array(1,array(1,array(1,array(1,array(1,array(1,array(1,array(1,array(1,array(1,array(1,0)))))))))))))); 
$test_arr[] = array(1,array(1,array(1,array(1,array(1,array(1,0)))))); 
wei($test_arr); 
echo max($wei); 
?>

解决方案3:

$arr = array(array( array(array(“5″, “6″), “7″, “8″)),2,array(array(array(“5″, “6″), “7″, “8″,array( “7″,“8″))),4); 
echo getArrDemp($arr); 
function getArrDemp($array){ 
    static $offset = 0; 
    $arr_str = serialize($array); 
    $num = substr_count($arr_str,'{'); 
    $result = array(); 
    for($i=0;$i<$num;$i++){ 
        $l_pos = strpos($arr_str, '{', $offset); 
        $temp_str = substr($arr_str,0,$l_pos); 
        $offset = $l_pos + 1; 
        $result[] = substr_count($temp_str,'{')-substr_count($temp_str,'}'); 
    } 
    array_multisort($result,SORT_DESC); 
    return ++$result[0]; 
}

解决方案4:递归实现

<?php 
function countdim($array) { 
   static $dimcount = 1; 
   if (is_array(reset($array))) { 
       $dimcount++; 
       $return = countdim(reset($array)); 
   } else { 
       $return = $dimcount; 
   } 
   return $return; 
} 
  
$array = array( array( array(array("5", "6"), "7", "8")),array( array(array("5", "6"), "7", "8")), "9", "10", array("11"), array()); 
  
echo countdim($array); 
?>

解决方案5:简单的计算深度函数:

<?php 
function array_depth($array) { 
        $max_depth = 1; 
  
        foreach ($array as $value) { 
            if (is_array($value)) { 
                $depth = array_depth($value) + 1; 
  
                if ($depth > $max_depth) { 
                    $max_depth = $depth; 
                } 
            } 
        }         
        return $max_depth; 
 } 
  
$array = array( array("11"), array(),array( array(array("5", "6"), "7", "8")),array( array(array("5", "6"), "7", "8")), "9", "10"); 
echo array_depth($array); 
?>

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