title: Creating a recursive category tree function
body: <p>I have a database with a bunch of categories, some are children:</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 1
              [name] =&gt; Home Improvement
              [slug] =&gt; Home-Improvement
              [parent] =&gt; 
              [user_id] =&gt; 1
              [order] =&gt; 1
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 2
              [name] =&gt; Asbestos Abatement &amp; Removal
              [slug] =&gt; Asbestos-Abatement-Removal
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 8
          )
  
      [2] =&gt; Array
          (
              [id] =&gt; 3
              [name] =&gt; Asphalt &amp; Asphalt Products
              [slug] =&gt; Asphalt-Asphalt-Products
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 9
          )
  
      [3] =&gt; Array
          (
              [id] =&gt; 4
              [name] =&gt; Bathroom
              [slug] =&gt; Bathroom
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 10
          )
  
      [4] =&gt; Array
          (
              [id] =&gt; 5
              [name] =&gt; Kitchen Cabinets
              [slug] =&gt; Kitchen-Cabinets
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 11
          )
  
      [5] =&gt; Array
          (
              [id] =&gt; 6
              [name] =&gt; Ceilings
              [slug] =&gt; Ceilings
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 12
          )
  
      [6] =&gt; Array
          (
              [id] =&gt; 7
              [name] =&gt; Cleaning
              [slug] =&gt; Cleaning
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 13
          )
  
      [7] =&gt; Array
          (
              [id] =&gt; 8
              [name] =&gt; Closet Organizers &amp; Accessories
              [slug] =&gt; Closet-Organizers-Accessories
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 14
          )
  
      [8] =&gt; Array
          (
              [id] =&gt; 9
              [name] =&gt; Concrete
              [slug] =&gt; Concrete
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 15
          )
  
      [9] =&gt; Array
          (
              [id] =&gt; 10
              [name] =&gt; Contractors &amp; Service Providers
              [slug] =&gt; Contractors-Service-Providers
              [parent] =&gt; 1
              [user_id] =&gt; 1
              [order] =&gt; 16
          )
  </code></pre>
  
  <p>What I'm trying to output is something like this:</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;Parent
          &lt;ul&gt;
              &lt;li&gt;Child&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Parent with no Children&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I'm trying to build a recursive tree script in PHP, but I'm stuck. Here's what I have so far. I'm stuck on what to do between the else: and endif; in the foreach. (And I'm using that syntax just for easier reading here.) Any suggestions?</p>
  
  <pre><code>echo $this-&gt;categories-&gt;makeTree(0, $this-&gt;db-&gt;get('categories')-&gt;result_array());
  
  public static function makeTree($parent, $array)
  {
    if (!is_array($array)) return '';
  
    $output = '&lt;ul&gt;';
  
    foreach($array as $key =&gt; $value):
      if ($value['parent'] == $parent):
          $output .= '&lt;li&gt;';
  
          if ($value['parent'] == NULL):
              $output .= $value['name'];
          else:
  
          endif;
      endif;
  
      $output .= '&lt;/li&gt;';
    endforeach;
  
    $output .= '&lt;/ul&gt;';
    return $output;
  }
  </code></pre>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I was able to get this working, although I have a database call in a foreach loop, which probably isn't the best idea:</p>
  
  <pre><code>public function makeTree($parent, $array)
  {
    if (!is_array($array)) return FALSE;
  
    $output = '&lt;ul&gt;';
  
    foreach($array as $key =&gt; $value):
      if ($value['parent'] == $parent):
          $output .= '&lt;li&gt;';
  
          if ($value['parent'] == NULL):
              $output .= $value['name'];
  
              $subcategories = ci()-&gt;db-&gt;get_where('categories', array('parent' =&gt; $value['id']));
  
              if ($subcategories-&gt;num_rows() &gt; 0):
                  $output .= $this-&gt;makeTree($value['id'], $subcategories-&gt;result_array());
              endif;
          else:
              $output .= $value['name'];
              $output .= '&lt;/li&gt;';
          endif;
      endif;
  
    endforeach;
  
    $output .= '&lt;/ul&gt;';
    return $output;
  }
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>Here is my final solution, reusing the array instead of doing a DB query:</p>
  
  <pre><code>public function makeTree($parent, $array)
  {
    if (!is_array($array) OR empty($array)) return FALSE;
  
    $output = '&lt;ul&gt;';
  
    foreach($array as $key =&gt; $value):
      if ($value['parent'] == $parent):
          $output .= '&lt;li&gt;';
  
          if ($value['parent'] == NULL):
              $output .= $value['name'];
  
              $matches = array();
  
              foreach($array as $subkey =&gt; $subvalue):
                  if ($subvalue['parent'] == $value['id']):
                      $matches[$subkey] = $subvalue;
                  endif;
              endforeach;
  
              $output .= $this-&gt;makeTree($value['id'], $matches);
  
          else:
              $output .= $value['name'];
              $output .= '&lt;/li&gt;';
          endif;
      endif;
  
    endforeach;
  
    $output .= '&lt;/ul&gt;';
  
    return $output;
  }
  </code></pre>