创建数组我需要遍历目录结构,并采用特殊结构推到阵列。所以,我有下一个目录结构从目录结构
<pre>
collection
|
|
---buildings
| |
| |
| ---greece
| | |
| | |
| | ---1.php
| | ---2.php
| |
| |
| ---rome
| |
| |
| ---1.php
| ---3.php
|
|
---trees
|
|
---evergreen
| |
| |
| ---1.php
| ---2.php
|
|
---leaves
|
|
---1.php
---2.php
</pre>
所以需要“解析”的,并准备数据,接下来的格式:
array('collection' => array('category' => 'buildings',
'subcategory' => 'greece',
'type' => 1),
array('category' => 'buildings',
'subcategory' => 'greece',
'type' => 2)),
array('category' => 'buildings',
'subcategory' => 'rome',
'type' => 1),
array('category' => 'buildings',
'subcategory' => 'rome',
'type' => 1),
array('category' => 'buildings',
'subcategory' => 'rome',
'type' => 3),
array('category' => 'trees',
'subcategory' => 'evergreen',
'type' => 1),
array('category' => 'trees',
'subcategory' => 'evergreen',
'type' => 2),
array('category' => 'trees',
'subcategory' => 'leaves',
'type' => 1),
array('category' => 'trees',
'subcategory' => 'leaves',
'type' => 2)
),
我觉得跟RecursiveDirectoryIterator来实现它。所以我将'path'作为参数传递给RecursiveDirectoryIterator。然后我传递了这个新对象ReursiveIteratorIterator。之后,我用'foreach'语句来迭代它。所以我创建下一个代码:
$path = __DIR__ . '/collection/';
$dir = new RecursiveDirectoryIterator($path);
$files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
if ($file->isDir()) {
if (0 === $files->getDepth()) {
$objects['category'] = $file->getBasename();
}
if (1 === $files->getDepth()) {
$objects['subcategory'] = $file->getBasename();
}
}
if ($file->isFile()) {
$objects['fileName'] = $file->getBasename('.php');
continue;
}
}
我期望接收所需数据的数组。 但这个代码只给我:
array('category' => 'buildings',
'subcategory' => 'greece',
'fileName' => '1'
)
请帮我才达到这个任务我的目标! 谢谢!
你见过这里例如https://stackoverflow.com/问题/ 14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu?answertab = active#tab-top with'RecursiveIteratorIterator :: CATCH_GET_CHILD' –
我无法理解它可以帮助我。如何获得'建筑物'目录。比看它是否有子目录。如果它具有名称,则添加到结果数组中。比看,如果孩子有目录文件,如果有,把它的名字,添加到结果阵列。再看一下,如果孩子目录有另一个文件。如果已添加到结果数组。我需要从子文件夹中取出所有文件,并添加到结果数组中。从那以后,我需要检查,如果父文件夹(“建筑”)有其他孩子folder.If它有我需要循环,添加到结果数组。从那以后,我需要采取另一种“收集”的文件夹,重复operations.Canü帮助我吗? –