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!

2
  • Have you seen that example here stackoverflow.com/questions/14304935/… with the RecursiveIteratorIterator::CATCH_GET_CHILD Commented Oct 15, 2017 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? Commented Oct 15, 2017 at 20:02

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.