扫描文件夹并从文件夹加载文件

Scan folders and load files from folder

我有一个文件夹结构:

- components
--com_name
---routes
----routes.json
--com_another_name
---routes
----routes.json
...
--com_x
---routes
----routes.json

如何加载每个 com_x 文件夹中的每个 routes.json 文件并合并到一个数组? 谢谢!

使用迭代器,如下所示。递归查找 json 个文件:

$path = realpath($pathToYourMainDir);

$directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$regexIterator = new RegexIterator($directoryIterator, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:json)$#Di');
$files = [];

foreach ($regexIterator as $file) {
    array_push($files, $file->getPathName());
}

$files 包含 json 个文件名(包括路径)。