title: PHP Multidimensional array to unordered list, building up url path
body: <p>I have a multidimensional array in PHP produced by the great examples of icio and ftrotter (I am use ftrotterrs array in arrays variant):</p>
  
  <p><a href="https://stackoverflow.com/questions/2794638/turn-database-result-into-array">Turn database result into array</a></p>
  
  <p>I have made this into a unordered list width this method:</p>
  
  <pre><code>public function outputCategories($categories, $startingLevel = 0)
  {
    echo "&lt;ul&gt;\n";
    foreach ($categories as $key =&gt; $category)
    {
      if (count($category['children']) &gt; 0)
      {
        echo "&lt;li&gt;{$category['menu_nl']}\n";
        $this-&gt;outputCategories($category['children'], $link
                                , $start, $startingLevel+1);
        echo "&lt;/li&gt;\n";
      }
      else
      {
        echo "&lt;li&gt;{$category['menu_nl']}&lt;/li&gt;\n";
      }
    }
    echo "&lt;/ul&gt;\n";
  }
  </code></pre>
  
  <p>So far so good.</p>
  
  <p>Now I want to use the url_nl field to build up the url's used as links in the menu. The url has to reflect the dept of the link in de tree by adding up /url_nl for every step it go's down in the tree.</p>
  
  <p>My goal:</p>
  
  <pre><code>- item 1 (has link: /item_1)
      * subitem 1 (has link: /item_1/subitem_1)
      * subitem 2 (has link: /item_1/subitem_1)
          * subsubitem 1 (has link: /item_1/subitem_2/subsubitem_1)
  - item 2 (has link: /item_2)
  </code></pre>
  
  <p>the table</p>
  
  <pre><code>id
  id1 (parent id)
  menu_nl
  url_nl
  title_nl
  etc
  </code></pre>
  
  <p>What I have so far:</p>
  
  <pre><code>public function outputCategories($categories, $link, $start, $startingLevel = 0)
  {
    // if start not exists
    if(!$start)
      $start = $startingLevel;
  
    echo "&lt;ul&gt;\n";
    foreach ($categories as $key =&gt; $category)
    {
      $link.= "/".$category['url_nl'];
  
      if($start != $startingLevel)
        $link = strrchr($link, '/');
  
      if (count($category['children']) &gt; 0)
      {
        echo "&lt;li&gt;".$start." - ".$startingLevel.
             "&lt;a href='$link'&gt;{$category['menu_nl']}&lt;/a&gt; ($link)\n";
        $this-&gt;outputCategories($category['children'], $link
                                , $start, $startingLevel+1);
        echo "&lt;/li&gt;\n";
      }
      else
      {
        $start = $startingLevel+1;
        echo "&lt;li&gt;".$start." - ".$startingLevel.
             "&lt;a href='$link'&gt;{$category['menu_nl']}&lt;/a&gt; ($link)&lt;/li&gt;\n";
      }
    }
    echo "&lt;/ul&gt;\n";
  }
  </code></pre>
  
  <p>As you see in the example I have used a url_nl field which is recursively added so every level of the list has a link with a path which is used as a url.</p>
  
  <p>Anyhow, I have problems with building up these links, as they are not properly reset while looping to the hierarchical list. After going down to the child in de list the first one is right but the second one not.</p>
  
  <p>I'm stuck here... </p>