简体   繁体   中英

All sub directories with content to array

Okay so I currently have a function to browse to all the directories and show the image on the page(As seen below)

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>';
    }
}

Now I'm wondering how to get the same idea(Get all folders and content(Images only, jpg in this case)) And add these image to an array similar to this:

Array ( 
[FolderName1] => Array ( 
    [0] => someimage.jpg 
    [1] => anotherimage.jpg 
     ) 
[FolderName2] => Array ( 
    [0] => moreimages.jpg 
    [1] => muchimages.jpg 
    ) 
)
$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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