在通过数组递归时需要帮助保持相对路径 - PHP

时间:2011-01-13 00:58:56

标签: php arrays recursion

我有一系列文件夹/文件,如下所示:

Array
(
    [Root Folder 1] => Array
        (
            [Sub Folder 1] => Array
                (
                    [:files:] => Array
                        (
                            [0] => file 1.txt
                            [1] => file 2.txt
                        )
                )

            [Sub Folder 2] => Array
                (
                    [:files:] => Array
                        (
                            [0] => file.txt
                        )
                )

            [Sub Folder 3] => Array
                (
                    [:files:] => Array
                        (
                            [0] => file.txt
                        )
                )

        )

    [Root Folder 2] => Array
        (
            [Sub Folder 1] => Array
                (
                    [:files:] => Array
                        (
                            [0] => file.txt
                        )
                )

            [Sub Folder 2] => Array
                (
                    [:files:] => Array
                        (
                            [0] => file.txt
                        )
                )

            [Sub Folder 3] => Array
                (
                    [:files:] => Array
                        (
                            [0] => file.txt
                        )
                )
        )
)

基本上,我有文件夹名称作为键,在文件夹中,有一个:files:键,其中包含该文件夹中所有文件的列表。

我遇到的问题是在回显数组数据时创建该文件的“完整路径”。例如,我需要看起来像这样的路径:

Root Folder 1\Sub Folder 1\file 1.txt
Root Folder 1\Sub Folder 1\file 1.txt
Root Folder 1\Sub Folder 2\file1.txt

我目前的功能如下:

function format_tree($array, $map, $currentPath=null)
{
    if (!is_array($array))
        return;

    foreach ($array as $key => $value)
    {
        if ($key != ':files:')
        {
            $currentPath = $currentPath . DS . $key;

            echo '<div class="folder"><span style="display: block; font-weight: bold" title="' . $currentPath . '"><span>' . $key . '</span></span><div class="contents">';

            format_tree($value, $map, $currentPath);

            echo '</div></div>';
        }
        else
        {
            for ($i = 0; $i < count($value); $i++)
            {
                $path = $currentPath . DS . $value[$i];
                echo '<span class="file" style="display: block" title="' . $path . '"><span><a href="/public/uploads/' . $path . '" target="_blank">' . $value[$i] . '</a></span></span>';
            }
        }
    }
}

问题是当您离开目录时$currentPath永远不会'重置'。这导致如下路径:

Root Folder 1\Sub Folder 1\file 1.txt
Root Folder 1\Sub Folder 1\file 1.txt
Root Folder 1\Sub Folder 1\Sub Folder 2\file1.txt

当数组中的当前键(文件夹名称)被附加到旧目录路径时。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

我不想在函数中回显一些html。这个将生成一个包含您想要的路径的数组。它还可以迭代更深层次的嵌套数组。背叛的魔力:)

function getfiles($paths, &$files = array(), $currentPath = '') 
{

    foreach ( $paths as $key => $path )
    {
        if ( $key == ':files:' )
        {
            foreach ( $path as $file )
            {
                $files[] = $currentPath . '/' .$file;
            }
            continue;
        }
        getfiles($path, $files, $currentPath . '/' . $key);
    }
    return $files;
}

// usage example, $paths is the array holding the nested structure

$list = array();
getfiles($paths, $list);
var_dump($list);

答案 1 :(得分:0)

$path = substr($path, 0, strrpos($path, '/') + 1);

这应该从路径字符串中删除最后一个子文件夹。