2016-05-18 64 views
0

我有一個數組,其中包括我的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

'//我自己的功能 function in_array_r($ needle,$ haystack,$ strict = false){foreach($ haystack as $ item){ if(($ strict?$ item === $ needle:$ item == $針)||(is_array($ item)&& in_array_r($ needle,$ item,$ strict))){ return true; } } return false; }' 這是我到目前爲止,但我迷路了。 – NewbTechGuy

回答

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>"; 


        }