我有2个独立的多维数组,它们具有相同的结构。例如:
Array
(
[0] => Array
(
[uploadData] => 1234 Main St
)
[1] => Array
(
[uploadData] => 5678 Elm St
)
[2] => Array
(
[uploadData] => 9879 New St
)
[3] => Array
(
[uploadData] => 9876 Shady Lane
)
)
Array
(
[0] => Array
(
[uploadData] => Orlando
)
[1] => Array
(
[uploadData] => Tampa
)
[2] => Array
(
[uploadData] => Miami
)
[3] => Array
(
[uploadData] => West Palm Beach
)
)
我只需要将它们变成一个看起来像这样的新数组:
Array
(
[0] => Array
(
[uploadData] => 1234 Main St Orlando
)
[1] => Array
(
[uploadData] => 5678 Elm St Tampa
)
[2] => Array
(
[uploadData] => 9879 New St Miami
)
[3] => Array
(
[uploadData] => 9876 Shady Lane West Palm Beach
)
)
我一直在尝试使用array_merge
,但我无法让它发挥作用。
答案 0 :(得分:3)
简单,递归,与密钥无关的解决方案:
function array_concat_recursive($array1, $array2){
$result = array();
foreach($array1 as $key => $value){
if(isset($array2[$key])){
if(is_array($value)){
$result[$key] = array_concat_recursive($value, $array2[$key]);
}else{
$result[$key] = $value . ' ' . $array2[$key];
}
}
}
return $result;
}
答案 1 :(得分:2)
假设:
$address
$city
修改强>
@ dev-null-dweller的精彩捕获 - 编辑以捕获数组的全部深度。
注意强>
有问题的示例代码没有关于uploadData键的引号,因此在此复制:
以下将做你想做的事:
foreach($address as $key=>$value) {
$newarray = array(
'uploadData'=>$value['uploadData'] . ' ' . $city[$key]['uploadData'];
);
}
$newarray
将包含根据您的请求构建的数组。
答案 2 :(得分:1)
从两个数组中获取,直到一切都消失为止:
$final = [];
$key = 'uploadData';
while ($array1 && $array2)
{
$final[][$key] = array_shift($array1)[$key] . ' ' . array_shift($array2)[$key];
}