title: I need a node to act as both node and leaf in Array with undefined depth
body: <p>I'm working on a comment system, in which one is able to reply to another comment, making it appear below the commented comment. I have a bunch of parent/child pairs, to which end I use recursion to convert this into a tree structure:</p>
  
  <pre><code>private function _create_tree_from_comments($comment_id, $entry_id) {
      $node_set = array();
      $children = $this-&gt;comment_model-&gt;get_children($comment_id, $entry_id);
  
      // check if we're a leaf
      if($children-&gt;num_rows() &lt; 1) {
          return $comment_id;
      }
  
  
      foreach($children-&gt;result() as $child) {
          $node_set[$child-&gt;child] = $this-&gt;_create_tree_from_comments($child-&gt;child, $entry_id);
      }
  
      return $node_set;
  }
  </code></pre>
  
  <p>Now this is all fine and well, but the problem is that I need to save information about intermediate nodes. Currently all I can do is save whatever information I need in another array at a leaf, since the intermediate nodes have responsibility to keep information about their children.</p>
  
  <p>So my question is really: How am I able to save information about intermediate nodes without breaking the relationship there is between the parent and its children. </p>
  
  <p>Some sample output of the function:</p>
  
  <pre><code>Array 
  (
       [5] =&gt; 5
       [2] =&gt; 2
       [1] =&gt; Array
           (
                 [4] =&gt; Array
                     (
                         [6] =&gt; 6
                     )
  
                 [3] =&gt; 3
           )
  
  )    
  </code></pre>