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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
在您使用子节点的情况下,您需要将结果除以 2。Greetz
,
XpertEase
Try this:
In you case with the child nodes, you will need to divide result by 2.
Greetz,
XpertEase