简体   繁体   English

在目录数组上使用scandir

[英]Using scandir on array of directories

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. 我认为这与我如何使用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 注意:未定义的索引:第6行的C:\\ xampp \\ htdocs \\ test.php中的C:\\ Users \\ john \\ Desktop \\ files \\ images \\

Warning: scandir(): Directory name cannot be empty in C:\\xampp\\htdocs\\test.php on line 6 警告:scandir():目录名在第6行的C:\\ xampp \\ htdocs \\ test.php中不能为空

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. 如果有人可以想到一种更简单的方法,请随时告诉我。

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. @BrianPoole所述,您需要将$list移出foreach循环。 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. 另外, @ TimCooper上面解释的foreach循环与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. 然后,您将使用$dir[$key]$bar访问目录值。

Finally, array_push is an additional function call that in your case is not needed. 最后, array_push是您不需要的其他函数调用。 By simply adding [] PHP will push the new value onto the end of the array. 通过简单地添加[] PHP会将新值推送到数组的末尾。

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. 它提供了一些检查并设置了数据,因此您可以更清楚地了解它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM