title: Why pushed array is not persisted?
body: <p>I am trying to build a nested tree out of linear paths,
  the paths I have are sorted from parent toward children, like this:</p>
  
  <pre><code>$paths= array(array(999, 10, 9, 8, 7), array(999, 10, 9, 82, 75), array(999, 10, 9, 83, 74));
  </code></pre>
  
  <p>where 10 is child of 999, 9 is a child of 10, and 8 is a child of 9, etc..</p>
  
  <p>The tree_root has 999 id, which will present the top node of the tree.</p>
  
  <pre><code>$tree_root = array('id' =&gt; 999, 'nodes' =&gt; array());
  </code></pre>
  
  <p>The problem I have is that the <code>buildTree</code> will not add persisted new children into the <code>tree_root</code>, insertion works ONLY inside tree_root direct children, any inserted nodes inside children of tree_root's children will not be there after returning from buildTree function, any idea how to retain references and solve this?</p>
  
  <pre><code>// loop of sorted paths:
  for ($i = 0; $i &lt; count($paths); $i++) {
    for ($l = 0; $l &lt; count($paths[$i]) - 1; $l++) {
      $this-&gt;buildTree($tree_root, $paths[$i][$l], $paths[$i][$l + 1]);
    }
  }
  
  public function buildTree(&amp;$a, $parent_id, $child_id)
      {
          if ($a["id"] == $parent_id) {
              $exists = false;
              for ($i = 0; $i &lt; count($a["nodes"]); $i++) {
                  if ($a["nodes"][$i]["id"] == $child_id) {
                      $exists = true;
                      break;
                  }
              }
              if (!$exists) {
                  $node_template_children = array();
                  $node_template = array('id' =&gt; $child_id, 'nodes' =&gt; &amp;$node_template_children);
                  $node_template = &amp;$node_template;
                  $a_nodes = &amp;$a["nodes"];
  
                  // $a_nodes is not affected when returning from buildTree!!
                  array_push($a_nodes, $node_template); 
              }
              return true;
          }
          foreach ($a["nodes"] as $node) {
              $result = $this-&gt;buildTree($node, $parent_id, $child_id);
              if ($result == true) {
                  return true;
              }
          }
          return false;
      }
  </code></pre>