使用PHP通过键和值内爆多维数组
我想将数组键连接到一个filepath,其末尾的值为file本身(下面的数组是一个“filetree”) 数组(深度、大小和键名是动态的): 这个结果很好:使用PHP通过键和值内爆多维数组,php,arrays,implode,Php,Arrays,Implode,我想将数组键连接到一个filepath,其末尾的值为file本身(下面的数组是一个“filetree”) 数组(深度、大小和键名是动态的): 这个结果很好: [0] => bla.tif [1] => quux.tif [2] => foo/bar/lorem/ipsum.tif [3] => foo/bar/lorem/doler.tif [4] => bar/qux/baz/ipsum.tif [5] => bar/qux/baz/ufo.tif 也许还
[0] => bla.tif
[1] => quux.tif
[2] => foo/bar/lorem/ipsum.tif
[3] => foo/bar/lorem/doler.tif
[4] => bar/qux/baz/ipsum.tif
[5] => bar/qux/baz/ufo.tif
也许还有一个纯粹的PHP解决方案。我用
array\u map
尝试了它,但是结果不够好。我会使用递归函数来折叠这个数组。下面是一个例子:
function collapse($path, $collapse, &$result)
{
foreach($collapse AS $key => $value)
{
if(is_array($value))
{
collapse($path . $key . "/", $value, $result);
continue;
}
$result[] = $path . $value;
}
}
下面是如何使用:
$result = array();
$toCollapse = /* The multidimentional array */;
collapse("", $toCollapse, $result);
而
$result
将包含“内爆”数组请使用递归迭代器进行尝试。有了这种结构,您将如何处理目录名实际上是一个数字的情况?@Yoshi,这种情况不会发生-所有目录名都已用字母命名。(这将不会更改。)
$result = array();
$toCollapse = /* The multidimentional array */;
collapse("", $toCollapse, $result);