简体   繁体   English

为什么array_filter函数仅适用于PHP循环中的最后一个目录?

[英]Why does the array_filter function work only for last directory in a loop in PHP?

While looping through various directories, I used array_filter to put the directories' filenames into an array, then we proceed to the next directory; 在遍历各种目录时,我使用array_filter将目录的文件名放入数组中,然后进入下一个目录。

The value of $HALLO1 is a value that is read from an array. $ HALLO1的值是从数组读取的值。

One thing I noticed is that when array_filter() is operated on $HALLO1 and in the case where $HALLO1 is equal to an array of directories, the var_dump() function does not list files in the first directories but only list the files contained in last directory. 我注意到的一件事是,当对$ HALLO1操作array_filter()时,并且在$ HALLO1等于目录数组的情况下,var_dump()函数不会列出第一个目录中的文件,而只会列出其中包含的文件。最后一个目录。 However, when $HALLO1 is equal to one and only directory the array_filter() works and lists all the files contained in that directory. 但是,当$ HALLO1等于1并且只有目录时,array_filter()起作用并列出该目录中包含的所有文件。 Why is that ? 这是为什么 ?

<?php

foreach ($CC1 as $directory)  {
    $GG1 = strval($CC1[$dd]);
    $HALLO1 = $GG1;
    echo "HALLO1 = " . $HALLO1 ;
    $iterator = new DirectoryIterator(dirname($HALLO1));
    //*****************************************************
    $f_files = array_filter(glob("$HALLO1*"), 'is_file');
    var_dump($f_files);
    //*****************************************************
    ++$dd; 
    } 

?>

Because you're overwriting $f_files in each iteration 因为您在每次迭代中都覆盖$ f_files

$f_files = array()
foreach ($CC1 as $directory)  {
    $GG1 = strval($CC1[$dd]);
    $HALLO1 = $GG1;
    $f_files = array_merge($f_files, array_filter(glob("$HALLO1*"), 'is_file'));
}
var_dump($f_files);

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

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