有没有办法找出如何"深" PHP数组是什么? [英] Is there a way to find out how "deep" a PHP array is?
本文介绍了有没有办法找出如何"深" PHP数组是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个PHP数组可以有它的元素数组。而这些阵列可以有数组等等等等。有没有办法找出存在于PHP数组的最大嵌套?一个例子是,返回1的函数,如果初始阵列不具有阵列作为元素,2如果至少一个元件是一个数组,等
A PHP array can have arrays for its elements. And those arrays can have arrays and so on and so forth. Is there a way to find out the maximum nesting that exists in a PHP array? An example would be a function that returns 1 if the initial array does not have arrays as elements, 2 if at least one element is an array, and so on.
推荐答案
这应该这样做:
<?php
function array_depth(array $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;
}
?>
编辑:非常快速地测试,它似乎工作。
Tested it very quickly and it appears to work.
这篇关于有没有办法找出如何&QUOT;深&QUOT; PHP数组是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文