0

I would like to pass in an array that contains a list of directories to scan. I want to iterate over each directory and push its content into another array that I will print out, but for some reason my code is not working. The directories exist and the path is correct. I think it has something to do with how I'm using foreach.

These are the errors I'm getting:

Notice: Undefined index: C:\Users\john\Desktop\files\images\ in C:\xampp\htdocs\test.php on line 6

Warning: scandir(): Directory name cannot be empty in C:\xampp\htdocs\test.php on line 6

This is the code:

function test($dir = []) {
    foreach($dir as $bar) {
        $list = [];
        array_push($list, scandir($dir[$bar]));
    }

    print_r($list);
}

test(["C:\Users\john\Desktop\files\images\\", "C:\Users\john\Desktop\files\images\autumn\\"]);

If anyone can think of a simpler way to do this, please don't hesitate to tell me.

5
  • 2
    PHP's foreach is not like JavaScript's for in statement; $bar will be the actual element, not the element's key. Commented Jun 10, 2014 at 18:24
  • What does $dir look like? Commented Jun 10, 2014 at 18:26
  • @TimCooper: Ah okay. But i'm only seeing the content of the second directory. I changed it to foreach($dir as $bar) { $list = []; array_push($list, scandir($bar)); } - would that be correct? Commented Jun 10, 2014 at 18:27
  • 1
    You're resetting $list with each directory. Move $list out of the foreach. Commented Jun 10, 2014 at 18:30
  • You just want to call scandir($bar) and append the result to $list using array_merge(). In your current form, you are resetting the content of the list each iteration. Note that if you wanted to iterate over keys and values, there is the foreach ($array as $key => $value) syntax, but I don't think there's any need for that here. Commented Jun 10, 2014 at 18:34

2 Answers 2

1

You're on the right track. There are a few changes you need to make though.

function test($dir = []) {
    $list = [];
    foreach($dir as $bar) {
        $list[] = scandir($bar);
    }  
    print_r($list);
}

As noted by @BrianPoole you need to move the $list out of the foreach loop. By having it in the loop, the array is reset with each iteration, resulting in the final array having one element.

In addition, the foreach loop as explained above by @TimCooper does not operate the same as in JavaScript. If you really want to access the keys, you can use the following syntax:

foreach($dir as $key => $bar)

You would then use either $dir[$key] or $bar to access the directory value.

Finally, array_push is an additional function call that in your case is not needed. By simply adding [] PHP will push the new value onto the end of the array.

Sign up to request clarification or add additional context in comments.

Comments

1
function test($dir) {
    // DEFINE LIST OUT SIDE OF LOOP
    $list = array();
    // Run checks
    if(count($dir) > 0) {
        // Loop Through Directory
        foreach($dir as $directory) {
            // Push into list
            array_push($list, array("scanned"=>$directory, "contents" => scandir($directory)));
        }
    }else {
        // If no directories are passed return array with error
        $list = array(
            "error" => 1,
            "message" => "No directories where passed into test()",
        );
    }
    print_r($list);
}

This is how I would do it. It provides a couple checks and sets up the data so you can se it a bit more clear.

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.