Home  >  Article  >  php教程  >  确定数组维度

确定数组维度

PHP中文网
PHP中文网Original
2016-05-25 17:06:51984browse

确定一个数组的维度

<?php
function array_depth($array) {
     if(!is_array($array)) return 0;
       $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( array(array(1, 2), 3, 4)));
echo array_depth($array);
?>
 
输出:4
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