小编Slo*_*Bon的帖子

foreach不填充数组

我试图递归扫描文件夹结构并填充一个数组,其中包含每个找到的文件的路径.但是print_r($fileArray)给我一个空数组作为输出?像这样:Array ( )

哪里出错了?

function getFilePaths($path){
    $structureArray = [];
    $fileArray = [];

    foreach(new DirectoryIterator($path) as $item){

        if($item->isDir()){
            if(!$item->isDot()) { 
                $structureArray[$item->getFilename()] = getFilePaths($item->getPathname());
            }
            continue;
        }

        $fileArray[] = '"'. $item->getPathname() .'"';
    } 

    return $fileArray;
}
Run Code Online (Sandbox Code Playgroud)

更新

var_dump($ item)的输出; 在你的foreach里面:

object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(68) "music\sorted\3 Doors Down\Seventeen Days\01 Right Where I Belong.mp3" ["fileName":"SplFileInfo":private]=> string(27) "01 Right Where I Belong.mp3" ["glob":"DirectoryIterator":private]=> bool(false) ["subPathName":"RecursiveDirectoryIterator":private]=> string(0) "" } object(DirectoryIterator)#9 (4) { ["pathName":"SplFileInfo":private]=> string(59) "music\sorted\3 Doors Down\Seventeen Days\02 It's Not Me.mp3" ["fileName":"SplFileInfo":private]=> …
Run Code Online (Sandbox Code Playgroud)

php arrays foreach directory-structure

2
推荐指数
1
解决办法
118
查看次数

标签 统计

arrays ×1

directory-structure ×1

foreach ×1

php ×1