Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create array from directory structure

Tags:

php

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!

like image 718
Antin Ju Avatar asked Dec 02 '25 20:12

Antin Ju


1 Answers

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>
like image 189
anguswild Avatar answered Dec 05 '25 10:12

anguswild



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!