title: Count number of leaves in nested array tree
body: <p>I have a nested array tree, generated from a flat array with the following function :</p>
  
  <pre><code>function convertToTree(array $flat, $idField = 'id',
                          $parentIdField = 'parentId',
                          $childNodesField = 'childNodes') {
      $indexed = array();
      // first pass - get the array indexed by the primary id  
      foreach ($flat as $row) {
          $indexed[$row[$idField]] = $row;
          $indexed[$row[$idField]][$childNodesField] = array();
      }
  
      //second pass  
      $root = null;
      foreach ($indexed as $id =&gt; $row) {
          $indexed[$row[$parentIdField]][$childNodesField][$id] =&amp; $indexed[$id];
          if (!$row[$parentIdField]) {
              $root = $id;
          }
  
      }
      return array($root =&gt; $indexed[$root]);
  }
  </code></pre>
  
  <p>I would need to add an entry "NUMBER OF LEAVES" for each node of my array. This entry should count ALL the leaves of all the subnodes of the node :</p>
  
  <pre><code>Array ( 
      [9] =&gt; Array ( 
          [id] =&gt; 9, 
          [parentId] =&gt; null,
          [name] =&gt; Item 0, 
          [NUMBER OF LEAVES] =&gt; 4,  (corresponding to leaves 100 and 101 + 200 and 201)
          [childNodes] =&gt; Array 
              ( 
                  [1] =&gt; Array ( 
                      [id] =&gt; 1, 
                      [parentId] =&gt; 9, 
                      [name] =&gt; Item 1, 
                      [NUMBER OF LEAVES] =&gt; 2,   (corresponding to leaves 100 and 101)
                      [childNodes] =&gt; Array ( 
                          [10] =&gt; Array ( 
                              [id] =&gt; 10, 
                              [parentId] =&gt; 1, 
                              [name] =&gt; Item 10, 
                              [childNodes] =&gt; Array ( 
                                  [100] =&gt; Array ( 
                                      [id] =&gt; 100, 
                                      [parentId] =&gt; 10, 
                                      [name] =&gt; Item 100, 
                                      [childNodes] =&gt; Array ( ) 
                                  ) 
                                  [101] =&gt; Array ( 
                                      [id] =&gt; 101, 
                                      [parentId] =&gt; 10, 
                                      [name] =&gt; Item 101, 
                                      [childNodes] =&gt; Array ( ) 
                                  ) 
                              ) 
                          ) 
                      ) 
                  ) 
                  [2] =&gt; Array ( 
                      [id] =&gt; 2, 
                      [parentId] =&gt; 9, 
                      [name] =&gt; Item 2, 
                      [NUMBER OF LEAVES] =&gt; 2,   (corresponding to leaves 200 and 201)
                      [childNodes] =&gt; Array ( 
                          [20] =&gt; Array ( 
                              [id] =&gt; 20, 
                              [parentId] =&gt; 2, 
                              [name] =&gt; Item 20, 
                              [childNodes] =&gt; Array ( 
                                  [200] =&gt; Array ( 
                                      [id] =&gt; 200, 
                                      [parentId] =&gt; 20, 
                                      [name] =&gt; Item 200, 
                                      [childNodes] =&gt; Array ( ) 
                                  ) 
                                  [201] =&gt; Array ( 
                                      [id] =&gt; 201, 
                                      [parentId] =&gt; 20, 
                                      [name] =&gt; Item 201, 
                                      [childNodes] =&gt; Array ( ) 
                                  ) 
                              ) 
                          ) 
                      ) 
                  ) 
              ) 
      ) 
  )
  </code></pre>