我有一个数组,其中包含我homedir中的所有目录。我正在运行一个foreach循环,我需要为我的下拉菜单输出“</ul>
”。我需要在父目录的最后一个子目录的末尾或有换行符的地方输出结束标记。 (我用下面的空格对数组进行分组。)
我不知道如何从逻辑上解决这个问题。我想也许一个递归函数会有所帮助,但我不知道从哪里开始。
Array ( [0] => Array ( [0] => test_php [1] => test_php )
[1] => Array ( [0] => bootstrap-336-dist [1] => bootstrap-3.3.6-dist )
[2] => Array ( [0] => .css [1] => bootstrap-3.3.6-dist/css )
[3] => Array ( [0] => .fonts [1] => bootstrap-3.3.6-dist/fonts )
[4] => Array ( [0] => .js [1] => bootstrap-3.3.6-dist/js )
[5] => Array ( [0] => dynasty_loop [1] => dynasty_loop )
[6] => Array ( [0] => .C1_00626E58960C [1] => dynasty_loop/C1_00626E58960C )
[7] => Array ( [0] => ..snap [1] => dynasty_loop/C1_00626E58960C/snap )
[8] => Array ( [0] => .C1_00626E5894EF [1] => dynasty_loop/C1_00626E5894EF )
[9] => Array ( [0] => ..snap [1] => dynasty_loop/C1_00626E5894EF/snap )
[10] => Array ( [0] => nvr_snapshots [1] => nvr_snapshots )
[11] => Array ( [0] => .snap [1] => nvr_snapshots/snap )
[12] => Array ( [0] => test_php2 [1] => test_php2 ) )
答案 0 :(得分:0)
我明白了。如果有人在寻找类似的东西,这是我的答案。我的foreach循环语法没有关闭,所以要小心。
foreach($_SESSION['menulite'] as $key => $url) {
$url_raw = $url;
$key_raw = $key;
$url[0]=trim($url[0],'.');
$nextmenuitem = $_SESSION['menulite'][$key + 1];
//if (substr($nextmenuitem[0],0,1)=='.') {
$padding_left='';
$padding_left = (substr_count($nextmenuitem[0], '.', 0, strpos($nextmenuitem[0], trim($nextmenuitem[0], '.'))) -1);
if ( strpos( $url_raw[0],'.' ) !== 0 && strpos( $nextmenuitem[0],'.' ) !== 0 ) {
$div1 = "<li role=\"presentation\">" ;
$div2 = "</li>";
}
if ( strpos( $url_raw[0],'.' ) !== 0 && strpos( $nextmenuitem[0],'.' ) === 0 ) {
$div1 = "<li role=\"presentation\" $class1> <a$dropdown> $url_raw[0] $caret </a> <ul class=\"dropdown-menu\">" ;
$div2 = null;
}
if ( strpos( $url_raw[0],'.' ) === 0 && strpos( $nextmenuitem[0],'.' ) === 0 ) {
$div1 = "<li>" ;
$div2 = "</li>";
}
if ( strpos( $url_raw[0],'.' ) === 0 && strpos( $nextmenuitem[0],'.' ) !== 0 ) {
$div1 = "<li>" ;
$div2 = "</li>";
//echo "</ul>";
}