我想在PHP中将2个数组合并为1。我在这个网站上搜索过类似的问题,但似乎无法找到答案。
Array 1
(
[0] => Array
(
[LOGIN] => 123
[CITY] => bangkok
[GROUP] => bangkok-a
[PREV_A] => 123.4
[PREV_B] => 456.7
)
[1] => Array
(
[LOGIN] => 456
[CITY] => bangkok
[GROUP] => bangkok-b
[PREV_A] => 987.6
[PREV_B] => 654.3
)
)
Array 2
(
[0] => Array
(
[LOGIN] => 123
[CITY] => bangkok
[GROUP] => bangkok-a
[A] => 555.5
[B] => 666.6
)
[1] => Array
(
[LOGIN] => 456
[CITY] => bangkok
[GROUP] => bangkok-b
[A] => 777.7
[B] => 888.8
)
)
我希望生成的数组变成这样:
Array 3
(
[0] => Array
(
[LOGIN] => 123
[CITY] => bangkok
[GROUP] => bangkok-a
[PREV_A] => 123.4
[PREV_B] => 456.7
[A] => 555.5
[B] => 666.6
)
[1] => Array
(
[LOGIN] => 456
[CITY] => bangkok
[GROUP] => bangkok-b
[PREV_A] => 987.6
[PREV_B] => 654.3
[A] => 777.7
[B] => 888.8
)
)
每个数组都是64.我尝试过这段代码,但结果数组有4096(= 64x64)个字段而不是64个。
foreach($arr1 as $x){
foreach($arr2 as $y){
if ($x['LOGIN']=$y['LOGIN']){
$tmp=array();
$tmp=array_merge($x,$y);
array_push($res,$tmp);
}
}
}
如何正确组合它们?谢谢。
答案 0 :(得分:2)
怎么样:
$newArray = Array();
foreach($arr1 as $k=>$val)
{
if(array_key_exists($k, $arr2))
{
$newArray[$k] = array_merge($val, $arr2[$k]);
}
}
尚未经过测试,但我认为它应该有效......