Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php上多维菜单中的文件列表_Php - Fatal编程技术网

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

php上多维菜单中的文件列表,php,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] => /U

我需要从文件列表中创建一个嵌套菜单。 列表如下所示:

[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属性中。

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

要将数组显示为菜单,需要另一个递归函数来构建嵌套的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);
函数构建菜单($nested\u数组)
{
回声“
    ”; foreach($folder\u name=>$array的嵌套数组) { 回音“
  • ”; if(is_数组($array)) { 回显“.$folder_name.”
    ; 构建菜单($array); } 其他的 { //从url中获取文件名(因此不显示完整的url: $path_parts=分解('/',$array); 回声'; } 回音“
  • ”; } 回声“
”; } 构建菜单($nested\u数组);
您是否尝试将.jpgs用作菜单图像?它们是指向图像的链接吗?您尝试了什么?是的,它们是指向图像的链接,插入到“href”中属性。我尝试了*foreach every line,但无法创建递归函数*foreach every line,分解它并附加到ul if line nestedMany谢谢!只使用了您的代码,一切正常!
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);