title: Recursively loop through multidimensional to create flat array
body: <p>I have a multidimensional array that looks like this:</p>
  
  <pre><code>$trees = array(
      array(
          'name' =&gt; 'Parent',
          '__children' =&gt; array(
              array(
                  'name' =&gt; 'Child'
              ),
              array(
                  'name' =&gt; 'Second Child'
              )
          )
      )
  );
  </code></pre>
  
  <p>The depth of the array is unknown and I need to recursively flatten it. So it looks more like this:</p>
  
  <pre><code>array(
    array(
      'name' =&gt; 'Parent' 
    ),
    array(
      'name' =&gt; 'Child' 
    ),
    array(
      'name' =&gt; 'Second Child' 
    )
  )
  </code></pre>
  
  <p>I thought something like this might work:</p>
  
  <pre><code>public function flattenTree($trees, $tree = array())
  {
      foreach($trees as $item){
          //$i = 1, 2, then 3
          $i = count($tree);
          $tree[$i] = array('name' =&gt; $item['name']);
          if(isset($item['__children']))
              $this-&gt;flattenTree($item['__children'], $tree);
      }
      return $tree;
  }
  </code></pre>
  
  <p>But this is only gives me :(</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [name] =&gt; Parent
          )
  
  )
  </code></pre>
  
  <p>I am unsure how to do this. Is it possible?</p>
  
  <p>As a bonus I really need the output array to look like this(notice the name value changed) :)</p>
  
  <pre><code>array(
    array(
      'name' =&gt; 'Parent' 
    ),
    array(
      'name' =&gt; 'Parent Child' 
    ),
    array(
      'name' =&gt; 'Parent Second Child' 
    )
  )
  </code></pre>
  
  <p>Thanks a ton for the help on this one. Looking forward to the solutions. I am stumped!</p>