dtn913117 2018-09-24 18:17
浏览 51
已采纳

如何使用glob创建子目录数组

I have the following directory structure:

MainFolder
    Folder1
        Folder1-1
             Folder1-1-1  
             Folder1-1-2
             Folder1-1-3    
        Folder1-2
             Folder1-2-1  
             Folder1-2-2
             Folder1-2-3  
    Folder2
        Folder2-1
             Folder2-1-1  
             Folder2-1-2
             Folder2-1-3  
        Folder2-2
             Folder2-2-1  
             Folder2-2-2
             Folder2-2-3

I'm trying to create 3 arrays

1 - array of all subfolders of MainFolder (Folder1, Folder2..etc)

2 - array of subfolders inside of Folder1, Folder2, etc (e.g:Folder1-1...folder2-1...)

3 - array of subfolders inside Folder1-1..., Folder1-2..., etc

this way I can only filter the subdirectories of the current directory:

//path to directory to scan
$directory = "MainFolder/";

//get all files in specified directory
$files = glob($directory . "*");

//print each file name
foreach($files as $file)
{
 //check to see if the file is a folder/directory
 if(is_dir($file))
 {
  echo $file;
 }
}

but how do I, for glob to filter the current directory and automatically group into array as I showed in the example?

I already saw that RecursiveDirectoryIterator exists but I did not understand how to put it in different arrays

  • 写回答

2条回答 默认 最新

  • dougai2427 2018-09-24 20:10
    关注

    You have a fixed low depth, so you don't really need recursive imho.

    You can use wildcard * to mark different levels and GLOB_ONLYDIR to retrieve folders only :

    $level1 = glob('MainFolder/*', GLOB_ONLYDIR);
    $level2 = glob('MainFolder/*/*', GLOB_ONLYDIR);
    $level3 = glob('MainFolder/*/*/*', GLOB_ONLYDIR);
    

    If you want to store the last folder instead of full path you can use array_map() and basename() :

    $level1 = array_map('basename', glob('MainFolder/*', GLOB_ONLYDIR));
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来