How do I determine the dimensions of an php array

  • 2020-06-12 08:43:43
  • OfStack

As shown below:

<?php 
/** 
 *  Returns the dimension 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); 
} 

$arr = array( 
    '0'=>'0', 
); 

echo arrayLevel($arr); 
?>

Related articles: