I would like to find the maximum depth of this array:
Array
(
[0] => Array
(
[children] => Array
(
[0] => Array
(
[children] => Array
开发者_开发技巧 (
[0] => Array
(
[children] =>
)
)
)
)
[children] => Array
(
[0] => Array
(
[children] =>
)
)
)
)
In this case it's 3 because one of the nodes contains two children nodes.
This is the code I have been trying so far:
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);
}
Try this:
<?php
function array_depth($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;
}
?>
In you case with the child nodes, you will need to divide result by 2.
Greetz,
XpertEase
精彩评论