title: How to store items recursively?
body: <p>I want to collect&amp; store data into an array while drilling into some structured data, so that when I am done, I have an array like this for ex::</p>
  
  <pre><code>[{nodes:[{nodes:[], id:2}, {nodes:[{nodes:[], id:4}], id:3} ], id:1}]
  </code></pre>
  
  <p>Notice how I store deeper nodes into parent nodes,Here is my attempt simplified, as I tried to pass node by reference to be able to collect deeper nodes into their parent nodes:</p>
  
  <pre><code>$parentNode= array('nodes'=&gt;array());
  scan ($parentNode, $excel, 0, 0);
  
  function scan (&amp; $parentNode, $excel, $row, $column, $maxColumn)
  {
      $childNode = array('nodes' =&gt; array(), 'id' =&gt; $excel[$row]['cr'.$column]);
  
      // to make sure I am drilling as expected, I used echo:
      echo ($newNode['id'].',');
      // now, push the child node into the passed by reference parent node:
      array_push($parentNode['nodes'], $childNode);
      // keep drilling, pass child node as parent:
      $this-&gt;scan($childNode, $excel, $row, $column + 1, $maxColumn);
  }
  </code></pre>
  
  <p>But, what I get is:</p>
  
  <pre><code>{nodes:[{nodes:[],id:1}]}
  </code></pre>
  
  <p>Why wouldn't I get what I expect? As long as I pass childNode by reference, I should be able to build my pyramid of nodes based on first passed node, right?</p>