title: Build recursive nested menu from array
body: <p>I'm trying to build a recursive menu which would look something like this when finished, with an infinite amount of nested items:</p>
  
  <pre class="lang-none prettyprint-override"><code>Homepage
  Services
    Members
    Brokers
    Insurers
  Products
  </code></pre>
  
  <p>My data is being linked together by a simple <code>parent_id</code> column in my table.</p>
  
  <p>I have currently managed to grab all the pages in the table and add them into an array based on their <code>parent_id</code> (see below).</p>
  
  <pre><code>Array
  (
      [0] =&gt; 0-0-1 0-1-2 0-2-12 0-3-27 0-4-28 0-5-29 0-6-30 0-7-31 0-8-33 
      [2] =&gt; 2-0-3 2-0-6 2-0-7 2-0-8 
      [3] =&gt; 3-0-4 3-0-5 
      [8] =&gt; 8-0-9 8-0-10 
      [12] =&gt; 12-0-13 12-0-20 
      [13] =&gt; 13-0-14 13-1-15 13-2-17 13-3-16 13-4-19 13-5-18 13-9-34 
      [20] =&gt; 20-0-21 20-1-22 20-2-24 20-3-23 20-4-26 20-5-25 20-6-11 
  )
  </code></pre>
  
  <p>This is formatted as [<code>parent_id</code>] => <code>parent_id</code>-<code>sort_order</code>-<code>id</code> <code>parent_id</code>-<code>sort_order</code>-<code>id</code> and so on.</p>
  
  <p>My code so far is as follows:</p>
  
  <pre><code>$sqlGetDropdownPages = "SELECT * FROM `cms_staticPages` ORDER BY `sortOrder` ASC";
  $qryGetDropdownPages = mysql_query($sqlGetDropdownPages) or die(mysql_error());
  $resGetDropdownPages = mysql_fetch_assoc($qryGetDropdownPages);
  $totGetDropdownPages = mysql_num_rows($qryGetDropdownPages);
  
  do {    
  
      $pageArray[$resGetDropdownPages['parent_id']] .= $resGetDropdownPages['parent_id'].'-'.$resGetDropdownPages['sortOrder'].'-'.$resGetDropdownPages['id'].' ';
  
  } while($resGetDropdownPages = mysql_fetch_assoc($qryGetDropdownPages));
  
  ksort($pageArray);
  
  foreach($pageArray as $i =&gt; $value) {
  
      $explodePages = explode(' ',$value);
  
      foreach(array_filter($explodePages) as $i2 =&gt; $page) {
  
          $pageInfoExplode = explode('-',$page);
  
          $getPageInfo = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_staticPages` WHERE `id` = '".$pageInfoExplode[2]."'"));
  
          echo $getPageInfo['identifier'].'&lt;br /&gt;';
  
      }
  
  }
  </code></pre>
  
  <p>Ran into a brick wall at this point and can't figure out what to do next.</p>
  
  <p>I tried to explain this as well as I possibly can so if any clarification is needed just ask.</p>
  
  <p>How to achieve this?</p>