2014-09-17 2 views
1

Хорошо, так что я в настоящее время имеют функцию для просмотра всех каталогов и показать изображение на странице (как показано ниже)Все подкаталоги с содержанием в массиве

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 
    ) 
) 

ответ

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); 
Смежные вопросы