多次元配列の深さを調べようとしています。配列は無制限のもの(深さ)です。配列の最大深度を知りたいです。以下の配列構造を参照してください
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;
}
?>
しかし、それはうまく機能していません
前もって感謝します。
よろしく、
スニル