title: Multi level menu with PHP/MySQL
body: <p>I'm trying to create dynamic multi level menus fetching the data from a MySQL DB, using PHP. I've managed to order the menu items in a php array with this format:</p>
  
  <pre><code>-----------------------
  Array
  (
  [1] =&gt; Array
      (
          [id] =&gt; 1
          [ubicacion] =&gt; top_a
          [nivel] =&gt; 1
          [parent_id] =&gt; 
          [tipo] =&gt; link
          [link] =&gt; http://www.google.com
          [titulo] =&gt; Google
          [alias] =&gt; google_es
          [children] =&gt; Array
              (
                  [3] =&gt; Array
                      (
                          [id] =&gt; 3
                          [ubicacion] =&gt; top_a
                          [nivel] =&gt; 2
                          [parent_id] =&gt; 1
                          [tipo] =&gt; link
                          [link] =&gt; http://www.gmail.com
                          [titulo] =&gt; Gmail
                          [alias] =&gt; gmail
                          [children] =&gt; Array
                              (
                                  [4] =&gt; Array
                                      (
                                          [id] =&gt; 4
                                          [ubicacion] =&gt; top_a
                                          [nivel] =&gt; 3
                                          [parent_id] =&gt; 3
                                          [tipo] =&gt; link
                                          [link] =&gt; www.inbox.gmail.com
                                          [titulo] =&gt; Inbox
                                          [alias] =&gt; inbox_gmail
                                      )
  
                              )
  
                      )
  
              )
      )
  
  [2] =&gt; Array
      (
          [id] =&gt; 2
          [ubicacion] =&gt; top_a
          [nivel] =&gt; 1
          [parent_id] =&gt; 
          [tipo] =&gt; link
          [link] =&gt; http://www.yahoo.com
          [titulo] =&gt; Yahoo
          [alias] =&gt; yahoo
      )
  )
  -----------------------
  </code></pre>
  
  <p>The problem is that I can't figure out how to output this array as HTML markup in a way that will work with n levels. I can do it with a fixed number of levels like this:</p>
  
  <pre><code>foreach($menu_array as $menu) {
   echo "&lt;li&gt;&lt;a href='{$menu['link']}'&gt;{$menu['titulo']}&lt;/a&gt;";
   if (array_key_exists('children',$menu)) {
      echo "&lt;ul&gt;";
      foreach ($menu['children'] as $child_menu) {
          echo "&lt;li&gt;&lt;a href='{$child_menu['link']}'&gt;{$child_menu['titulo']}&lt;/a&gt;";
          if (array_key_exists('children',$child_menu)) {
              echo "&lt;ul&gt;";
              foreach ($child_menu['children'] as $child2_menu) {
                  echo "&lt;li&gt;&lt;a href='{$child2_menu['link']}'&gt;{$child2_menu['titulo']}&lt;/a&gt;";
              }
              echo "&lt;/ul&gt;";
          }
      }
      echo "&lt;/ul&gt;";
  }
  echo "&lt;/li&gt;";
  }
  </code></pre>
  
  <p>But this only works for 3 levels, and I know there should be a way to solve this issue, I know I'm not the first one facing a problem with HTML output of a multidimensional array.</p>