PHP 如何无限极数组映射成文件夹?-灵析社区

雾里

好久没写 PHP 了,有点儿生疏,看看大概意思就行了: function flatten($nodes, $prefix = '') { $list = []; foreach ($nodes as $key => $value) { if (is_array($value)) { $path = $prefix . $key; $list[] = $path; $list = array_merge($list, flatten($value, $path . '/')); } } return $list; } flatten($config); ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250113/e9d2b9e326b3eb529334e444a5c4d0da.png) * * * 用 RecursiveIteratorIterator 也行,但需要指定第二个参数 `RecursiveIteratorIterator::SELF_FIRST`: $list = []; $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($config), RecursiveIteratorIterator::SELF_FIRST ); foreach ($iterator as $key => $value) { if (is_array($value)) { $path = ''; foreach (range(0, $iterator->getDepth()) as $depth) { $path .= $iterator->getSubIterator($depth)->key() . '/'; } $list[] = rtrim($path, '/'); } } ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/images/20250113/2c20a879703319248cf72587bebc52bc.png)

阅读量:1

点赞量:0

问AI