// create the caching iterator of the nav array
 $it = new RecursiveIteratorIterator(new RecursiveCachingIterator(new RecursiveArrayIterator($nav)), RecursiveIteratorIterator::SELF_FIRST);
 // child flag
 $depth = 0;
 // generate the nav
 foreach ($it as $name => $url) {
     // set the current depth
     $curDepth = $it->getDepth();
     // store the difference in depths
     $diff = abs($curDepth - $depth);
     // close previous nested levels
     if ($curDepth < $depth) {
         $output->append(str_repeat('</ul></li>', $diff));
     }
     // check if we have the last nav item
     if ($it->hasNext()) {
         $output->append('<li><a href="' . $url . '">' . $name . '</a>');
     } else {
         $output->append('<li class="last"><a href="' . $url . '">' . $name . '</a>');
     }
     // either add a subnav or close the list item
     if ($it->hasChildren()) {
         $output->append('<ul>');
     } else {
         $output->append('</li>');
     }
     // cache the depth
     $depth = $curDepth;
 }
 // if we have values, output the unordered list
 if ($output->count()) {