創建數組我需要遍歷目錄結構,並採用特殊結構推到陣列。所以,我有下一個目錄結構從目錄結構
<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ü幫助我嗎? –