从目录结构创建的多维数组

时间:2015-07-29 11:55:53

标签: php arrays multidimensional-array

我正在尝试整理一个脚本来读取目录并创建一个多维数组,其结构和内容与文件系统上的结构相同。

过去我只运行了一个遍历目录的脚本,并将密钥设置为完整路径,将值设置为在该路径中找到的文件。这次像这样的数组不会那么容易使用。

我使用那个旧脚本来获取我正在使用的文件结构:

Array
(
    [0] => files/example
    [1] => files/example/2015
    [2] => files/example/2015/Documents
    [3] => files/example/2015/Documents/Catalogs
    [4] => files/example/2015/Documents/Manuals
    [5] => files/example/2015/Documents/Press-Releases
    [6] => files/example/2015/Documents/Sell-Sheets
    [7] => files/example/2015/Email-Templates
    [8] => files/example/2015/Email-Templates/HTML-Templates
    [9] => files/example/2015/Email-Templates/Outlook-Templates
    [10] => files/example/2015/Email-Templates/Template-Image-Files
    [11] => files/example/2015/Images
    [12] => files/example/2015/Images/Ads-and-Flyers
    [13] => files/example/2015/Images/Catalog-and-PoP-Graphics
    [14] => files/example/2015/Images/Files-for-Web
    [15] => files/example/2015/Images/High-Resolution-Files
    [16] => files/example/2015/Images/Lifestyle-Shots
    [17] => files/example/2015/Images/Logos-and-Brand-Identity
    [18] => files/example/2015/Images/Product-Images
    [19] => files/example/2015/Images/Vector-Files
    [20] => files/example/2015/Other-Files
)

最终/ files下会有更多文件夹,而/ files的每个子文件夹中会有多年。

在我的搜索中,我发现人们提出了将单个路径转换为多维数组的方法,但我无法成功地将这些方法中的任何一个适应我的情况。有些是部分工作,有些根本不起作用。我最接近这个问题就是这个代码:

function getfiles($path,&$files){
    if(($h = opendir($path))){
        $args = explode('/',$path);
        $folder = array_pop($args);
        if(!isset($files[$folder])) $files[$folder] = array();
        while(($file = readdir($h)) !== false){
            $check = $path . '/' . $file;

            // directory
            if(is_dir($check)){
                if(!in_array($file,array('.','..'))){
                    getfiles($check, $files[$folder]);
                }

            // file
            }else if(!is_dir($check)){
                $files[$folder][] = $file;
            }
        }
    }
}

不幸的是,这不太对。它给了我这个:

    Array(
    [example] => Array(
            [2015] => Array(
                    [Documents] => Array(
                            [Press-Releases] => Array()
                            [Sell-Sheets] => Array()
                            [Catalogs] => Array()
                            [Manuals] => Array()
                        )
                    [Images] => Array(
                            [Lifestyle-Shots] => Array()
                            [Files-for-Web] => Array()
                            [High-Resolution-Files] => Array()
                            [Product-Images] => Array()
                            [Catalog-and-PoP-Graphics] => Array()
                            [Ads-and-Flyers] => Array()
                            [Vector-Files] => Array()
                            [Logos-and-Brand-Identity] => Array()
                        )
                    [Other-Files] => Array()
                    [Email-Templates] => Array(
                            [Outlook-Templates] => Array()
                            [Template-Image-Files] => Array()
                            [HTML-Templates] => Array()
                        )
                )
        )

    [2015] => Array(
            [Documents] => Array(
                    [Press-Releases] => Array()
                    [Sell-Sheets] => Array()
                    [Catalogs] => Array()
                    [Manuals] => Array()
                )
            [Images] => Array(
                    [Lifestyle-Shots] => Array()
                    [Files-for-Web] => Array()
                    [High-Resolution-Files] => Array()
                    [Product-Images] => Array()
                    [Catalog-and-PoP-Graphics] => Array()
                    [Ads-and-Flyers] => Array()
                    [Vector-Files] => Array()
                    [Logos-and-Brand-Identity] => Array()
                )
            [Other-Files] => Array()
            [Email-Templates] => Array(
                    [Outlook-Templates] => Array()
                    [Template-Image-Files] => Array()
                    [HTML-Templates] => Array()
                )
        )
    [Documents] => Array(
            [Press-Releases] => Array()
            [Sell-Sheets] => Array()
            [Catalogs] => Array()
            [Manuals] => Array()
        )
    [Catalogs] => Array()
    [Manuals] => Array()
    [Press-Releases] => Array()
    [Sell-Sheets] => Array()
    [Email-Templates] => Array(
            [Outlook-Templates] => Array()
            [Template-Image-Files] => Array()
            [HTML-Templates] => Array()
        )
    [HTML-Templates] => Array()
    [Outlook-Templates] => Array()
    [Template-Image-Files] => Array()
    [Images] => Array(
            [Lifestyle-Shots] => Array()
            [Files-for-Web] => Array()
            [High-Resolution-Files] => Array()
            [Product-Images] => Array()
            [Catalog-and-PoP-Graphics] => Array()
            [Ads-and-Flyers] => Array()
            [Vector-Files] => Array()
            [Logos-and-Brand-Identity] => Array()
        )
    [Ads-and-Flyers] => Array()
    [Catalog-and-PoP-Graphics] => Array()
    [Files-for-Web] => Array()
    [High-Resolution-Files] => Array()
    [Lifestyle-Shots] => Array()
    [Logos-and-Brand-Identity] => Array()
    [Product-Images] => Array()
    [Vector-Files] => Array()
    [Other-Files] => Array()
)

如您所见,它为每个文件夹创建一个根数组键,而不是仅停留在examples文件夹中。

我确信我的功能有一个简单的逻辑错误导致了这个问题,但要么我太接近它而没有注意到它,或者我的大脑不再与我合作了。

或者那不是我正在尝试做的正确方法。

提前感谢您提供的任何帮助。

2 个答案:

答案 0 :(得分:0)

呃,我是个白痴。我的功能没有任何问题,就像我使用它一样。

我正在遍历我在问题中发布的第一个数组来构建我的多维数组。这就是为什么我发布的最后一个数组充满了每个文件夹作为根数组键。

不知道我在想什么。 (答案:我不是......)

而不是:

$files = array()
$struct = traverse('files/example');
foreach($struct as $path){
    getfiles($path,$files);
}

我需要做的就是:

$files = array();
getfiles('files/example',$files);

答案 1 :(得分:0)

我使用RecursiveDirectoryIteratorRecursiveIteratorIteratoreval

以简单的方式制作了它
$rootpath = '.';
$fileinfos = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootpath));

$tree = array();

foreach($fileinfos as $pathname => $fileinfo) {
    if ($fileinfo->isFile()) {
        continue;
    }

    $arrayNotation = str_replace("/", "']['", str_replace(array('/.', '/..', '.'), '', $pathname));

    $str = <<<STRING
if (!isset(\$tree['$arrayNotation'])) {
    \$tree['$arrayNotation'] = array();
}
STRING;

    eval($str);
}

echo '<pre>', var_dump($tree);