[英]dynamic array_merge
I have an array which looks like this: 我有一个看起来像这样的数组:
array(2) {
[0]=> array(2) {
[0]=> string(52) "./app/pictures/uploads/Audi/A1/name1.jpg"
[1]=> string(52) "./app/pictures/uploads/Audi/A1/name2.jpg"
}
[1]=> array(1) {
[0]=> string(52) "./app/pictures/uploads/Audi/A3/name3.jpg"
}
}
The array above can have more than the two keys(0,1). 上面的数组可以具有两个以上的键(0,1)。 A little bit more information, would be that I look through a folder.
多一点信息,可能是我浏览一个文件夹。 If there are subfolders, it puts every subfolder in an array and the content/files of those subfolders in that arrays.
如果有子文件夹,它将每个子文件夹放入一个数组中,并将这些子文件夹的内容/文件放入该数组中。
So for my result I need something like this: 所以对于我的结果,我需要这样的东西:
array(3) {
[0]=> string(52) "./app/pictures/uploads/Audi/A1/name1.jpg"
[1]=> string(52) "./app/pictures/uploads/Audi/A1/name2.jpg"
[2]=> string(52) "./app/pictures/uploads/Audi/A3/name3.jpg"
}
I realized that with an array_merge
: 我意识到与
array_merge
:
$array = array_merge($tmparray[0],$tmparray[1]);
Now you can see that the keys in here are fixed. 现在您可以看到此处的键已固定。 But they should be dynamic.
但是它们应该是动态的。 How can I realize that?
我怎么能知道呢? Maybe a loop but I didn't get the clue, that the
$array
variable isn't overridden every time in that loop... 也许是一个循环,但我不知道,在该循环中每次都不会重写
$array
变量...
Maybe it's too late to have a clear mind for that but I need a solution very soon. 对此有一个清晰的想法也许为时已晚,但我很快需要一个解决方案。
You just need to loop over the parent array and then merge the children into an auxiliary variable: 您只需要循环遍历父数组,然后将子级合并到一个辅助变量中:
$result = array();
foreach ($directories as $array) {
$result = array_merge($result, $array);
}
Assume $directories is your multi-level array and $merged is what you want. 假设$ directories是您的多级数组,而$ merged是您想要的。 Then:
然后:
$merged = array();
foreach($directories as $dir) {
foreach($dir as $file) {
$merged[] = $file;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.