2017-10-15 167 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>