2013-11-27 52 views
-1

我需要從文件列表創建嵌套菜單。 名單看​​起來像:將文件列表轉換爲php的多維菜單

[0] => /Unit 10/Lesson 1/.jpg 
[1] => /Unit 10/Lesson 2/.jpg 
[2] => /Unit 10/Lesson 3/Homework/.jpg 
[3] => /Unit 10/Lesson 4/.jpg 
[4] => /Unit 10/Lesson 5/5/.jpg 
[5] => /Unit 10/Lesson 5/6/.jpg 
[6] => /Unit 10/Lesson 5/7/.jpg 
[7] => /Unit 10/Lesson 5/Homework/11/.jpg 
[8] => /Unit 10/Lesson 5/Homework/A/.jpg 
[9] => /Unit 10/Lesson 5/Homework/B/.jpg 
[10] => /Unit 10/Lesson 5/Homework/C/.jpg 
[11] => /Unit 10/Lesson 5/Homework/E/.jpg 
[12] => /Unit 10/Lesson 6/Workbook/3/1.jpg 
[13] => /Unit 10/Lesson 6/Workbook/3/2.jpg 
[14] => /Unit 10/Lesson 6/Workbook/3/3.jpg 

,我需要建立一個這樣的菜單:

Unit 10 > 
    Lesson 1.jpg 
    Lesson 2.jpg 
    Lesson 3 > 
     Homework.jpg 
    Lesson 4.jpg 
    Lesson 5 > 
     5.jpg 
     6.jpg 
     7.jpg 
     Homework > 
      11.jpg 
      A.jpg 
      B.jpg 
      C.jpg 
      E.jpg 
    Lesson 6 > 
     Workbook > 
      3 > 
       3.jpg 
       4.jpg 
       5.jpg 

我浪費了很多時間解決它,任何人都可以幫我嗎? 每個.jpg插入到標籤的href屬性中。

+0

您是否嘗試使用.jpgs作爲菜單圖像?他們是否鏈接到圖像?你有什麼嘗試? –

+0

是的,它們是圖像的鏈接,插入到「href」屬性中。 我試過 * foreach每一行,但無法創建遞歸函數 * foreach每一行,爆炸它並附加到ul如果行嵌套 – user3043178

回答

1

爲了實現這一點,您需要使用遞歸函數。像這樣的東西(爲我的作品):

// Get the list of files somehow (I assume you already have that): 
$list_of_files = array(
'/Unit 10/Lesson 1.jpg', 
'/Unit 10/Lesson 2.jpg', 
'/Unit 10/Lesson 3/Homework.jpg', 
'/Unit 10/Lesson 4.jpg', 
'/Unit 10/Lesson 5/5.jpg', 
'/Unit 10/Lesson 5/6.jpg', 
'/Unit 10/Lesson 5/7.jpg', 
'/Unit 10/Lesson 5/Homework/11.jpg', 
'/Unit 10/Lesson 5/Homework/A.jpg', 
'/Unit 10/Lesson 5/Homework/B.jpg', 
'/Unit 10/Lesson 5/Homework/C.jpg', 
'/Unit 10/Lesson 5/Homework/E.jpg', 
'/Unit 10/Lesson 6/Workbook/3/1.jpg', 
'/Unit 10/Lesson 6/Workbook/3/2.jpg', 
'/Unit 10/Lesson 6/Workbook/3/3.jpg', 
); 


function build_array_recursive(&$array_to_insert_into, $path_string, $file_url) 
{ 

// Break the path string into an array of maximum 2 elements using the '/' delimiter: 
$path_arr = explode('/', $path_string, 2); 

// If you have 2 elements, the first one will be a folder: 
if (count($path_arr) == 2) 
{ 
    // Check if the folder's array already exists, create it if not: 
    if (!isset($array_to_insert_into[$path_arr[0]])) 
    { 
     $array_to_insert_into[$path_arr[0]] = array(); 
    } 

    // Call the same function to go one level deeper in your nested array: 
    build_array_recursive($array_to_insert_into[$path_arr[0]], $path_arr[1], $file_url); 

} 
// If there's only one element that means you've reached the end of the string, and all you have left is the filename. Simply push it into the parent array: 
elseif (count($path_arr) == 1) 
{ 
    $array_to_insert_into[] = $path_arr[0]; 
} 

} 

// This will be your final nested array: 
$nested_array = array(); 

// Iterate over the file paths: 
foreach($list_of_files as $path_string) 
{ 
    // Remove the first '/' from the beginning of the string, if exists (this is to avoid having an array with an empty key on the first level: 
if ($path_string[0] == '/') 
{ 
    $path_string = substr($path_string, 1); 
} 

// Call recursive function to build the nested array: 
build_array_recursive($nested_array, $path_string, $path_string); 

來顯示陣列作爲一個菜單,你需要另一個遞歸函數建立嵌套的UL /李HTML標籤和顯示的鏈接文件:

function build_menu($nested_array) 
{ 
echo '<ul>'; 

foreach($nested_array as $folder_name => $array) 
{ 
    echo '<li>'; 

    if (is_array($array)) 
    { 
     echo '<b>' . $folder_name . '</b><br>'; 
     build_menu($array); 
    } 
    else 
    { 
     // get the file name out of the url (so you don't show the full url: 
     $path_parts = explode('/', $array); 
     echo '<a href="' . $array . '">' . array_pop($path_parts) . '</a>'; 
    } 

    echo '</li>'; 
} 

echo '</ul>'; 
} 

build_menu($nested_array); 
+0

非常感謝!剛剛使用你的代碼,一切正常! – user3043178