title: Build a bootstrap drop-down multi-level menu from a PHP array
body: <p>I'm trying to make a bootstrap drop-down menu from an array in PHP.</p>
  
  <p>I have written a recursive menu function, but I am struggling with adding the custom html attributes required for bootstrap.</p>
  
  <p>My menu array is in the following format:</p>
  
  <pre><code>$menu = array(
     'calendar' =&gt; array(
        'text'   =&gt; 'Calendar',
        'rights' =&gt; 'user'
     ),
     'customers' =&gt; array(
        'text'   =&gt; 'Customers',
        'rights' =&gt; 'user',
        'sub' =&gt; array(
           'create-new' =&gt; array(
              'text'   =&gt; 'Create new customer',
              'rights' =&gt; 'user'
           ),
           'show-customers' =&gt; array(
              'text'   =&gt; 'Show all customers',
              'rights' =&gt; 'user'
           )
        )
     )
  );
  </code></pre>
  
  <p>And the PHP to build the menu from an array as above:</p>
  
  <pre><code>function buildMenu($menu_array, $is_sub=FALSE) {
  
     $attr = (!$is_sub) ? ' id="menu"' : ' class="submenu"';
     $menu = "&lt;ul".$attr."&gt;";
  
     foreach($menu_array as $id =&gt; $properties) {
        foreach($properties as $key =&gt; $val) {
           if(is_array($val)) {
              $sub = buildMenu($val, TRUE);
           }
           else {
              $sub = NULL;
              $$key = $val;
           }
        }
        if(!isset($url)) {
           $url = $id;
        }
        $menu .= "&lt;li&gt;&lt;a href=".$url."&gt;".$text."&lt;/a&gt;".$sub."&lt;/li&gt;";
        unset($url, $text, $sub);
     }
  
     return $menu . "&lt;/ul&gt;";
  }
  
  echo $output = buildMenu($menu);
  </code></pre>
  
  <p>My desired output is:</p>
  
  <pre><code>&lt;ul class="nav navbar-nav"&gt;
      &lt;li&gt;&lt;a href="#"&gt;Calendrier&lt;/a&gt;&lt;/li&gt;
      &lt;li class="dropdown"&gt;
          &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown"&gt;Customers &lt;span class="caret"&gt;&lt;/span&gt;&lt;/a&gt;
          &lt;ul class="dropdown-menu"&gt;
              &lt;li&gt;&lt;a href="#"&gt;Create new customer&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="#"&gt;Show all customers&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>