我正在尝试组合一个脚本来读取目录并创建一个多维数组,该数组的结构和内容与文件系统上的结构相同。
在过去,我只是运行一个遍历目录的脚本,并将键设置为完整路径,将值设置为在该路径中找到的文件。这一次,像这样的数组就不那么容易处理了。
我使用了这个旧脚本来获得我在这里使用的文件结构:
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文件夹中。
我确信我的函数中有一个简单的逻辑错误导致了这一点,但要么我太接近它而没有注意到它,要么我的大脑就是不再与我合作。
或者,对于我想要做的事情,这不是正确的方法。
提前感谢您提供的任何帮助。
发布于 2015-07-29 20:04:19
呃,我是个笨蛋。我的函数没有问题,只是我使用它的方式有问题。
我遍历了我在问题中发布的第一个数组,以构建我的多维数组。这就是为什么我上一次发布的数组充满了作为根数组键的每个文件夹。
不知道我在想什么。(回答:我没有...)
而不是:
$files = array()
$struct = traverse('files/example');
foreach($struct as $path){
getfiles($path,$files);
}我所需要做的就是:
$files = array();
getfiles('files/example',$files);发布于 2015-07-29 20:52:55
我已经使用RecursiveDirectoryIterator、RecursiveIteratorIterator和eval以一种简单的方式实现了它
$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);https://stackoverflow.com/questions/31699762
复制相似问题