开发者

Need help keeping a relative path when recursing through an array - PHP

I have an array of folders/files that looks like this:

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
                        )
       开发者_如何学C         )

        )

    [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
                        )
                )
        )
)

Basically, the I have folder names as the keys and within the folders, there is a :files: key that contains a list of all files within that folder.

What I'm having trouble with is creating a 'full path' to that file when echoing at the array data. For example, I need paths to look like this:

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

My current function looks like this:

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>';
            }
        }
    }
}

The problem with it is the $currentPath never gets 'reset' when you leave a directory. This results in paths like this:

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

As the current key in the array (folder name) gets appended to the old directory path.

How do I fix this?


I didn't want to echo some html in a function . This one will generate an array containing those paths you wanted. It's also able to iterate over deeper nested arrays. Magic of recusion :)

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


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

this should remove the last subfolder from the path string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜