Question

I have a specific question. I'm new on php so it still gets a bit tough for me :)

So, I have a function which shoud scan dirs recursivly and print a specific array from result. I almost understand why it is happening, but I could not figure out how to change it like I need to. Please help!

At the moment I'm getting an array:

Array
(
    [0] => Array
        (
            [album1] => Array
                (
                    [0] => .
                    [1] => ..
                )
        )
    [1] => Array
        (
            [album2] => Array
                (
                    [0] => .
                    [1] => ..
                )
        )
    [2] => Array
        (
            [album3] => Array
                (
                    [0] => .
                    [1] => ..
                )
        )
)

But I need to get:

Array
    (
     [album1] => Array (
                        [0] => .
                        [1] => ..
                    )
     [album2] => Array
                    (
                        [0] => .
                        [1] => ..
                    )
     [album3] => Array
                    (
                        [0] => .
                        [1] => ..
                    )
    )

The function itself is:

 <?php

    function RAgetFiles($main_dir, $result = array()) {

        $dirs = scandir($main_dir); 
            foreach($dirs as $dir)  {
                if (is_dir("$main_dir/$dir")){

                    if ($dir === '.' || $dir === '..') { 
                        continue; }
                    $files=scandir($main_dir."/".$dir);
                    $result[] = array($dir => $files);
                }
        }
        return $result;
    }

    $rafiles = RAgetFiles('thumbs');
    echo '<pre>';
    print_r($rafiles);
    echo '<pre>';
?>
Was it helpful?

Solution

<?php
    function RAgetFiles($main_dir, $result = array()) {
        $dirs = scandir($main_dir); 
            foreach($dirs as $dir)  {
                if (is_dir("$main_dir/$dir")){
                    if ($dir === '.' || $dir === '..') { 
                        continue; }
                    $files=scandir($main_dir."/".$dir);
                    $result[$dir] = $files;
                }
        }
        return $result;
    }
$rafiles = RAgetFiles('/usr');
    echo '<pre>';
    print_r($rafiles);
    echo '</pre>';
?>

Works perfectly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top