从目录结构创建数组

时间:2017-10-15 16:24:23

标签: php

我需要迭代目录结构并将其推送到具有特殊结构的数组。所以我有下一个目录结构

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

请帮助我完成这项任务的目标! 谢谢!

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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;
    }
?>