问题描述
我需要将 2 个多维数组合并在一起以创建一个新数组.
这 2 个数组是从 $_POST 和 $_FILES 创建的,我需要它们相互关联.
I need to merge 2 multidimensional arrays together to create a new array.
The 2 arrays are created from $_POST and $_FILES and I need them to be associated with each other.
数组#1
Array ( [0] => Array ( [0] => 123 [1] => "Title #1" [2] => "Name #1" ) [1] => Array ( [0] => 124 [1] => "Title #2" [2] => "Name #2" ) )
数组#2
Array ( [name] => Array ( [0] => Image001.jpg [1] => Image002.jpg ) )
新数组
Array ( [0] => Array ( [0] => 123 [1] => "Title #1" [2] => "Name #1" [3] => "Image001.jpg" ) [1] => Array ( [0] => 124 [1] => "Title #2" [2] => "Name #2" [3] => "Image002.jpg" ) )
我正在使用的当前代码有效,但仅适用于数组中的最后一项.
我假设通过循环 array_merge 函数,它会在每个循环中擦除我的新数组.
The current code i'm using works, but only for the last item in the array.
I'm presuming by looping the array_merge function it wipes my new array every loop.
$i=0; $NewArray = array(); foreach($OriginalArray as $value) { $NewArray = array_merge($value,array($_FILES['Upload']['name'][$i])); $i++; }
我该如何纠正?
推荐答案
$i=0; $NewArray = array(); foreach($OriginalArray as $value) { $NewArray[] = array_merge($value,array($_FILES['Upload']['name'][$i])); $i++; }
[] 会将它附加到数组中而不是覆盖.
the [] will append it to the array instead of overwriting.