php上的多维菜单中的文件列表


List of files into multidimensional menu on 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属性中。

要实现这一点,您需要使用递归函数。像这样的东西(对我有用):

// 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/li 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);