2017-10-15 210 views
3

創建數組我需要遍歷目錄結構,並採用特殊結構推到陣列。所以,我有下一個目錄結構從目錄結構

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

請幫我才達到這個任務我的目標! 謝謝!

+0

你見過這裏例如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' –

+0

我無法理解它可以幫助我。如何獲得'建築物'目錄。比看它是否有子目錄。如果它具有名稱,則添加到結果數組中。比看,如果孩子有目錄文件,如果有,把它的名字,添加到結果陣列。再看一下,如果孩子目錄有另一個文件。如果已添加到結果數組。我需要從子文件夾中取出所有文件,並添加到結果數組中。從那以後,我需要檢查,如果父文件夾(「建築」)有其他孩子folder.If它有我需要循環,添加到結果數組。從那以後,我需要採取另一種「收集」的文件夾,重複operations.Canü幫助我嗎? –

回答

0

我也是用下面的代碼來獲得文件夾結構

<?php 
    $path = 'C:\assets';//absolute path 
    $path_array = getPathArr($path); 
    echo "<pre>"; 
    print_r($path_array); 
    echo "</pre>"; 
    function getPathArr($path) 
    { 
     $folders = scandir($path); 
     $new = array(); 
     foreach ($folders as $folder) { 
      if (!in_array($folder,['.','..'])) { 
       if (is_dir("$path/$folder")) { 
        $new[] = [$folder => getPathArr("$path/$folder")]; 
       } else { 
        $new[] = $folder; 
       } 
      } 
     } 
     return $new; 
    } 
?> 
1

我通常使用這個函數來獲取文件夾結構

<pre> 
<?php 


function dirToArray($dir) { 
    $contents = array(); 
    foreach (scandir($dir) as $node) { 
     if ($node == '.' || $node == '..') continue; 
     if (is_dir($dir . '/' . $node)) { 
      $contents[$node] = dirToArray($dir . '/' . $node); 
     } else { 
      $contents[] = $node; 
     } 
    } 
    return $contents; 
} 

$startpath = "path"; 

$r = dirToArray($startpath); 
print_r($r); 


?> 
</pre>