PHP Multidimensional array to unordered list, building up url path

耗尽温柔 提交于 2019-12-04 05:28:37

It looks like you modify the $link variable inside the foreach loop, So you add item1 to $link, loop thru its subitems and return to the first iteration and add item2 to the variable...

replace this

$link   .= "/".$category['url_nl']; 

with

$insidelink   = $link . "/".$category['url_nl']; 

(and change remaining $link inside the loop to $insidelink)

Adding: This is also true for $startingLevel. Do not modify it, use +1 inline:

echo "<li>".$start." - ".$startingLevel +1.
    "<a href='$link'>{$category['menu_nl']}</a> ($link)</li>\n";
TMan

Here is an easier way:

$inarray = your multi-dimensional array here. I used directory_map in codeigniter to get contents of directory including it's subdirectories.

$this->getList($filelist2, $filelist);
foreach ($filelist as $key => $val) {
    echo $val;
}

function getList($inarray, &$filelist, $prefix='') {
    foreach ($inarray as $inkey => $inval) {
        if (is_array($inval)) {
            $filelist = $this->getList($inval, $filelist, $inkey);
        } else {
            if ($prefix)
                $filelist[] = $prefix . '--' . $inval;
            else
                $filelist[] = $inval;
        }
    }

    return $filelist;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!