1
我想找到這個陣的最大深度:PHP:節點深度
Array
(
[0] => Array
(
[children] => Array
(
[0] => Array
(
[children] => Array
(
[0] => Array
(
[children] =>
)
)
)
)
[children] => Array
(
[0] => Array
(
[children] =>
)
)
)
)
在這種情況下,它是3,因爲其中一個節點包含兩個子節點。
這是我一直在努力,到目前爲止代碼:
public static function nodeDepth($nodes) {
$node_depth = array();
foreach($nodes as $node) {
foreach($node['children'] as $childnode) {
$node_depth[] = nodeDepth($childnode)+1;
}
}
return max($node_depth);
}