title: Recursive PHP function writing to external array
body: <p>Two questions here really; why is this happening? And what can be done about it?</p>
  
  <p>Sorry the question's so long but most of it is just <code>print_r</code> output!</p>
  
  <p>Basically, I start with a flat array of tags (<code>$tags</code>) each with an <code>id</code> (array index), <code>name</code> and <code>parent_id</code>. I then recursively iterate over <code>$tags</code> and nest all child tags within their parent. (see below)</p>
  
  <p>It works! (see below for output). But the problem I'm having is that my flat array of tags is being written to from within the function that does the nesting/recursion. (see below)</p>
  
  <p>Flat array of tags:</p>
  
  <pre><code>Array
  (
      [1] =&gt; stdClass Object
          (
              [name] =&gt; instruments
              [parent_id] =&gt; 0
          )
  
      [2] =&gt; stdClass Object
          (
              [name] =&gt; strings
              [parent_id] =&gt; 1
          )
  
      [3] =&gt; stdClass Object
          (
              [name] =&gt; violin
              [parent_id] =&gt; 2
          )
  
      [4] =&gt; stdClass Object
          (
              [name] =&gt; cello
              [parent_id] =&gt; 2
          )
  
      [5] =&gt; stdClass Object
          (
              [name] =&gt; woodwind
              [parent_id] =&gt; 1
          )
  
      [6] =&gt; stdClass Object
          (
              [name] =&gt; flute
              [parent_id] =&gt; 5
          )
  )
  </code></pre>
  
  <p>This is the recursively called function to nest the child tags. The problem is inside the <code>if</code>: I assign <code>$tag</code> to <code>$tree[$i]</code> and then add the <code>children</code> property to it. This results in the <code>children</code> property being added to <code>$tag</code>. This is what I want to stop happening.</p>
  
  <pre><code>public function tags_to_tree($tags, $parent_id = 0)
  {
      $tree = array();
      foreach($tags as $i =&gt; $tag)
      {
          // add this tag node and all children (depth-first recursive)
          if(intval($parent_id) === intval($tag-&gt;parent_id))
          {
              $tree[$i] = $tag;
              $tree[$i]-&gt;children = $this-&gt;tags_to_tree($tags, $i);
          }
      }
      return $tree;
  }
  </code></pre>
  
  <p>Nested tags output:</p>
  
  <pre><code>Array
  (
      [1] =&gt; stdClass Object
          (
              [name] =&gt; instruments
              [parent_id] =&gt; 0
              [children] =&gt; Array
                  (
                      [2] =&gt; stdClass Object
                          (
                              [name] =&gt; strings
                              [parent_id] =&gt; 1
                              [children] =&gt; Array
                                  (
                                      [3] =&gt; stdClass Object
                                          (
                                              [name] =&gt; violin
                                              [parent_id] =&gt; 2
                                          )
  
                                      [4] =&gt; stdClass Object
                                          (
                                              [name] =&gt; cello
                                              [parent_id] =&gt; 2
                                          )
                                  )
                          )
  
                      [5] =&gt; stdClass Object
                          (
                              [name] =&gt; woodwind
                              [parent_id] =&gt; 1
                              [children] =&gt; Array
                                  (
                                      [6] =&gt; stdClass Object
                                          (
                                              [name] =&gt; flute
                                              [parent_id] =&gt; 5
                                          )
                                  )
                          )
                  )
          )
  )
  </code></pre>
  
  <p>What can I do differently when adding the <code>children</code> property to <code>$tree[$i]</code>, or when assigning <code>$tag</code> to <code>$tree[$i]</code> to stop this from happening?</p>
  
  <p>Thanks!</p>