需要团队的帮助,
我有这种情况,每个数组中有两个相同的键,具有不同的值,我希望它们合并为一个键,值也在其中
示例:
arrayData1(
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
)
)
arrayData2(
[2] => Array
(
[EXP3] => Array (records...)
[EXP4] => Array (records...)
)
)
输出如下:
arrayFinal (
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
[EXP3] => Array (records...)
[EXP3] => Array (records...)
)
)
谢谢!
答案 0 :(得分:0)
首先,你不能在一个数组中有两个相同的键,你可以做的是使用php中的array_merge_recursive函数来合并两个数组,重复键将有一个包含所有重复键值的新数组。
$array1 = [
'EXP1' => [1,2,3],
'EXP2' => [2,3,4]
];
$array2 = [
'EXP2' => [5,6,7],
'EXP3' => [8,9,10]
];
现在有两个EXP2键,所以当你使用array_merge_recursive()时你会得到这样的东西,
print_r(array_merge_recursive($array1, $array2));
//output Array ( [EXP1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [EXP2] => Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 ) [EXP3] => Array ( [0] => 8 [1] => 9 [2] => 10 )
)