title: Recursive function to generate multidimensional array from database result
body: <p>I'm looking to write a function that takes an array of pages/categories (from a flat database result) and generates an array of nested page/category items based on the parent ids. I would like to do this recursively, so that any level of nesting can be done.</p>
  
  <p>For example: I'm fetching all the pages in one query, and this is the what the database table looks like</p>
  
  <pre><code>+-------+---------------+---------------------------+
  |   id  |   parent_id   |           title           |
  +-------+---------------+---------------------------+
  |   1   |       0       |   Parent Page             |
  |   2   |       1       |   Sub Page                |
  |   3   |       2       |   Sub Sub Page            |
  |   4   |       0       |   Another Parent Page     |
  +-------+---------------+---------------------------+
  </code></pre>
  
  <p>And this is the array I would like to end up with to process in my view files:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 1
              [parent_id] =&gt; 0
              [title] =&gt; Parent Page
              [children] =&gt; Array
                          (
                              [0] =&gt; Array
                                  (
                                      [id] =&gt; 2
                                      [parent_id] =&gt; 1
                                      [title] =&gt; Sub Page
                                      [children] =&gt; Array
                                                  (
                                                      [0] =&gt; Array
                                                          (
                                                              [id] =&gt; 3
                                                              [parent_id] =&gt; 1
                                                              [title] =&gt; Sub Sub Page
                                                          )
                                                  )
                                  )
                          )
          )
      [1] =&gt; Array
          (
              [id] =&gt; 4
              [parent_id] =&gt; 0
              [title] =&gt; Another Parent Page
          )
  )
  </code></pre>
  
  <p>I've looked and tried nearly every solution I've come across (there's a lot of them here on Stack Overflow, but have had no luck getting something generic enough that will work for both pages and categories.</p>
  
  <p>Here's the closest I've gotten, but it doesn't work because I'm assigning the children to the first level parent.</p>
  
  <pre><code>function page_walk($array, $parent_id = FALSE)
  {   
      $organized_pages = array();
  
      $children = array();
  
      foreach($array as $index =&gt; $page)
      {
          if ( $page['parent_id'] == 0) // No, just spit it out and you're done
          {
              $organized_pages[$index] = $page;
          }
          else // If it does, 
          {       
              $organized_pages[$parent_id]['children'][$page['id']] = $this-&gt;page_walk($page, $parent_id);
          }
      }
  
      return $organized_pages;
  }
  
  function page_list($array)
  {       
      $fakepages = array();
      $fakepages[0] = array('id' =&gt; 1, 'parent_id' =&gt; 0, 'title' =&gt; 'Parent Page');
      $fakepages[1] = array('id' =&gt; 2, 'parent_id' =&gt; 1, 'title' =&gt; 'Sub Page');
      $fakepages[2] = array('id' =&gt; 3, 'parent_id' =&gt; 2, 'title' =&gt; 'Sub Sub Page');
      $fakepages[3] = array('id' =&gt; 4, 'parent_id' =&gt; 3, 'title' =&gt; 'Another Parent Page');
  
      $pages = $this-&gt;page_walk($fakepages, 0);
  
      print_r($pages);
  }
  </code></pre>