我试图找出多维数组的深度。数组是无限的(深度)。我想找出阵列的最大深度。请参阅下面的数组结构
Array
(
[0] => Array
(
[id] => 27
[cata_key] => 55437c82626479a7b8554532
[cata_name] => Road
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 0
[subcategories] => Array
(
[0] => Array
(
[id] => 28
[cata_key] => 6f031dbf1b3a641689277aee
[cata_name] => Four Wheelers
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 27
[subcategories] => Array
(
[0] => Array
(
[id] => 29
[cata_key] => b6c6ef585ba8e3618e05155e
[cata_name] => Cars
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 28
[subcategories] => Array
(
[0] => Array
(
[id] => 30
[cata_key] => 6f3e2469551ad89c7f724b9f
[cata_name] => Hyundai
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 29
[subcategories] => Array
(
)
)
[1] => Array
(
[id] => 31
[cata_key] => f2f345824d547e08121b6d54
[cata_name] => Honda
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 29
[subcategories] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 32
[cata_key] => 74cec939a64cef188fb04458
[cata_name] => Two Wheelers
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 27
[subcategories] => Array
(
[0] => Array
(
[id] => 33
[cata_key] => 2c31a2bdb12458a4537f3c7a
[cata_name] => Hero Honda
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 32
[subcategories] => Array
(
[0] => Array
(
[id] => 39
[cata_key] => e55b2b38218f055b01cd58aa
[cata_name] => Splender
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 33
[subcategories] => Array
(
)
)
)
)
[1] => Array
(
[id] => 34
[cata_key] => dbe8c85a694913a33c73acb9
[cata_name] => Bajaj
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 32
[subcategories] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 35
[cata_key] => 05f450e3fe710f186517e61a
[cata_name] => River
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 0
[subcategories] => Array
(
[0] => Array
(
[id] => 36
[cata_key] => 8cc784f715135661c42733c2
[cata_name] => Boats
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 35
[subcategories] => Array
(
[0] => Array
(
[id] => 37
[cata_key] => 2668efe22245c3e0a897edf8
[cata_name] => 2 seater
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 36
[subcategories] => Array
(
)
)
[1] => Array
(
[id] => 38
[cata_key] => d90fd97291506cd823713543
[cata_name] => 6 seater
[app_key] => 3bb4f64af3d25034f0ae35bb
[parentid] => 36
[subcategories] => Array
(
)
)
)
)
)
)
)
所需的输出为4
我已经尝试了
<?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;
}
&GT;
但它运作不佳
提前致谢。
此致
苏尼
答案 0 :(得分:2)
这是我的看法:
<?php
$arr = array(
'a' => array(
'b' => array(
'c' => array(
'd' => array(
'e' => array()//5 deep
)
),
'c2' => array(
'a' => array(
'b' => array(
'c' => array(
'd' => array()//7 deep
)
)
)
)
)
)
);
function get_array_depth($arr, $n = 0) {
$max = $n;
foreach ($arr as $item) {
if (is_array($item)) {
$max = max($max, get_array_depth($item, $n + 1));
}
}
return $max;
}
$depth = get_array_depth($arr);
echo $depth;
?>
答案 1 :(得分:0)
<?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;
}
echo array_depth(array(array(array('a' => 1))));
?>
这会产生3.(使用您的代码 - 只需重新格式化。)
BTW:考虑对循环结构采取某种安全措施。答案 2 :(得分:-2)
试试这个男人
function array_depth($array, $n = 0) {
$max_depth = 1;
foreach ($array as $value) {
if (isset($value['subcategories'][0])) {
$depth = $this -> array_depth($value['subcategories']) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}