我正试图找出多维数组的深度。数组是无限的一个(深度(。我想找出阵列的最大深度。请参阅下方的阵列结构
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
函数array_deth(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;
}
?>
但运行不好
提前谢谢。
问候,
Sunil
这将是我对它的看法:
<?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;
?>
<?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:考虑对循环结构采取某种安全措施。
试试这个男人的
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;
}