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
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论