【问题标题】:Scan folders and load files from folder [closed]扫描文件夹并从文件夹加载文件[关闭]
【发布时间】:2015-12-15 19:34:14
【问题描述】:

我有一个文件夹结构:

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

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

【问题讨论】:

  • 你有没有尝试过实现目标?

标签: php arrays json recursion array-merge


【解决方案1】:

使用迭代器,如下所示。递归查找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 文件名(包括路径)。

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2012-03-16
    相关资源
    最近更新 更多