包含内容到数组的所有子目录

时间:2014-09-17 16:17:24

标签: php arrays

好的,所以我目前有一个浏览所有目录并在页面上显示图像的功能(如下所示)

recursiveGlob('wp-content/plugins/myplugin/v1/images/backgrounds', 'jpg', $weburl);



function recursiveGlob($dir, $ext, $weburl) {
    $globFiles = glob("$dir/*.$ext");
    $globDirs  = glob("$dir/*", GLOB_ONLYDIR);
    var_dump($globDirs);

    foreach ($globDirs as $dir) {
        recursiveGlob($dir, $ext);
    }

    foreach ($globFiles as $file) {
        $loc = str_replace('wp-content/plugins/myplugin/v1/', '', $file);
        echo '<li class="uploadedimage default-image"><img src="' . $weburl . $loc .'" alt="default-usable-image" /></li>';
    }
}

现在我想知道如何获得相同的想法(获取所有文件夹和内容(仅限图片,在这种情况下为jpg)) 并将这些图像添加到与此类似的数组中:

Array ( 
[FolderName1] => Array ( 
    [0] => someimage.jpg 
    [1] => anotherimage.jpg 
     ) 
[FolderName2] => Array ( 
    [0] => moreimages.jpg 
    [1] => muchimages.jpg 
    ) 
)

1 个答案:

答案 0 :(得分:0)

$result_array = array();
foreach($globDirs as $d){
    foreach($globFiles as $file){
        // Before push files in this array, check whether this file is in this folder or not.
        // If that file is in that folder then add it.
        if(//your condition...){
            array_push($result_array[$d], $file);
        }
    }
}
print_r($result_array);