Antin Ju
Antin Ju

Reputation: 51

Create array from directory structure

I need to iterate directory structure and push it to array with special structure. So I have next directories structure

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

So need to 'parse' its and prepare data in next format:

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

I think to implement it with RecursiveDirectoryIterator. So I passed 'path' as parameter to RecursiveDirectoryIterator. Then I passed this new object ReursiveIteratorIterator. After that I used 'foreach' statement to iterate it. So I create next code:

$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;
    }
}

I expected to receive arrays of needed data. But this code gives me only:

array('category' => 'buildings',    
      'subcategory' => 'greece',    
      'fileName' => '1'
)    

Please, help me to achive my goal in this task! Thank you!

Upvotes: 3

Views: 1556

Answers (2)

anguswild
anguswild

Reputation: 323

I usually use this function to get folder structure

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

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34914

I am also using below code to get folder structure

<?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;
    }
?>

Upvotes: 0

Related Questions