title: Creating menu list in PHP: from nested list to multidimensional array and return
body: <p>I have to create a menu from a list with possible sub-lists.
  From a simple  I want to create a compatible bootstrap menu
  So from a code like this:</p>
  
  <pre><code>&lt;ul&gt;
    &lt;li&gt;Link 1&lt;/li&gt;
    &lt;li&gt;Link 2
      &lt;ul&gt;
        &lt;li&gt;Sublink A&lt;/li&gt;
        &lt;li&gt;Sublink B&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;Link 3
      &lt;ul&gt;
        &lt;li&gt;Sublink C&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>I'll have to create this:</p>
  
  <pre><code>array(
    [0] =&gt; array(
      [id]       =&gt; 1,
      [nome]  =&gt; 'Link 1',
      [parent] =&gt; NULL,
    ),
    [1] =&gt; array(
      [id]       =&gt; 2,
      [nome]  =&gt; 'Link 2',
      [parent] =&gt; NULL,
    ),
    [2] =&gt; array(
      [id]       =&gt; 3,
      [nome]  =&gt; 'Sublink A',
      [parent] =&gt; 2
    ),
    [3] =&gt; array(
      [id]       =&gt; 4,
      [nome]  =&gt; 'Sublink B',
      [parent] =&gt; 2
    ),
    [4] =&gt; array(
      [id]       =&gt; 5,
      [nome]  =&gt; 'Link 3',
      [parent] =&gt; NULL
    ),
    [5] =&gt; array(
      [id]       =&gt; 6,
      [nome]  =&gt; 'Sublink C',
      [parent] =&gt; 3
    )
  )  
  </code></pre>
  
  <p>And finally, get back this one:</p>
  
  <pre><code>&lt;ul class="nav navbar-nav"&gt;
    &lt;li&gt;&lt;a class="primary" href="link_1.html"&gt;Link 1&lt;/a&gt;&lt;/li&gt;
    &lt;li class="dropdown"&gt;
      &lt;a href="#" class="primary dropdown-toggle" data-toggle="dropdown"&gt;Link 2&lt;/a&gt;
      &lt;ul class="dropdown-menu"&gt;
        &lt;li&gt;&lt;a href="sublink_a.html"&gt;Sublink A&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href="sublink_b.html"&gt;Sublink B&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li class="dropdown"&gt;
      &lt;a href="#" class="primary dropdown-toggle" data-toggle="dropdown"&gt;Link 3&lt;/a&gt;
      &lt;ul class="dropdown-menu"&gt;
        &lt;li&gt;&lt;a href="sublink_c.html"&gt;Sublink C&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Conditions are:
  if <li> has children  has href="#", add class dropdown-toogle and data-toggle="dropdown";
  child  has class "dropdown-menu";
  if <li> hasn't parent add class "primary" to  </p>
  
  <p>I use this code, but it's only for a simple list, not nested lists.</p>
  
  <pre><code>&lt;?php
  $currentPage = "";
  $contents = "&lt;ul&gt;
    &lt;li&gt;Link 1&lt;/li&gt;
    &lt;li&gt;Link 2&lt;/li&gt;
    &lt;li&gt;Link 3&lt;/li&gt;
  &lt;/ul&gt;";
  
  // remove width, height, style
  $contents = preg_replace('/(width|height|style|target)="[^"]+"/i', "", $contents);
  // remove spaces form li
  $contents = str_replace(array('&lt;li &gt;',' &lt;li &gt;'),"&lt;li&gt;",$contents);
  // remove ul/ol tag and tabs
  $contents = str_replace(array('\t','&lt;ul&gt;','&lt;ul &gt;','&lt;/ul&gt;','&lt;ol&gt;','&lt;ol &gt;','&lt;/ol&gt;'),"",$contents);
  $arrNavTopList = explode("\n",trim($contents));
  echo "&lt;h4&gt;Array:&lt;/h4&gt;\n";
  print_r($arrNavTopList);
  echo "&lt;hr&gt;\n&lt;h4&gt;List:&lt;/h4&gt;\n";
  $totNavTopList = count($arrNavTopList);
  if($totNavTopList&gt;0){
    echo '&lt;ul class="nav navbar-nav"&gt;'."\n";
    $countNtL=1;
    foreach($arrNavTopList as $pagename) {
      $pagename = str_replace("\t","",$pagename);
      preg_match_all("(&lt;li&gt;(.*?)&lt;/li&gt;)", $pagename, $arrLinkList);
      $linktopage = $arrLinkList[1][0];
      if(
        strtolower($linktopage)==strtolower(str_replace("_"," ",$currentPage)) ||
        strtolower($linktopage)=="home" &amp;&amp; !(strpos($currentPage,"^")===FALSE)
      ) {
        $active=' class="active"';
      } else {
        $active='';
      }
      echo '&lt;li'.$active.'&gt;';
      if (strstr($linktopage,"http") || strstr($linktopage,"target")) {
        $linktopage = preg_replace('/(style|target)="[^"]+"/i', "", $linktopage);
        $linktopage = str_replace('&lt;a','&lt;a rel="external nofollow" class="_blank"',$linktopage);
        echo $linktopage;
      } else {
        if(strtolower($linktopage)=="home" || strtolower($linktopage)=="home page") {
          echo '&lt;a href="/"&gt;'.htmlentities($linktopage).'&lt;/a&gt;';
        } else {
          echo '&lt;a href="'.str_replace(" ","_",strtolower($linktopage)).'"&gt;'.htmlentities($linktopage).'&lt;/a&gt;';
        }
      }
      echo '&lt;/li&gt;'."\n";
      $countNtL++;
    }
    echo '&lt;/ul&gt;'."\n";
  }
  ?&gt;
  </code></pre>