3

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!

Antin Ju
  • 51
  • 4
  • Have you seen that example here https://stackoverflow.com/questions/14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu?answertab=active#tab-top with the `RecursiveIteratorIterator::CATCH_GET_CHILD ` – A l w a y s S u n n y Oct 15 '17 at 16:33
  • I can't understand how it helps me.How can I get 'buildings' directory. Than look if it has child directory. If it has-take it name, add to the result array. Than look if child dir has file, If has-take it name, add to the result array. Than look again if child dir has another file. If has add to result array. I need take all file from child folder, add to result array. After that I need to check if parent folder ('buildings') has other children folder.If it has I need to iterate, add to result array. After that I need to take another 'collection' folder and repeat operations.Can U help me? – Antin Ju Oct 15 '17 at 20:02

2 Answers2

1

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>
anguswild
  • 323
  • 4
  • 16
0

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;
    }
?>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109