1

我想在 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);
        }
    }
}

如何正确组合它们?谢谢你。

4

1 回答 1

2

怎么样:

$newArray = Array();
foreach($arr1 as $k=>$val)
{
    if(array_key_exists($k, $arr2))
    {
        $newArray[$k] = array_merge($val, $arr2[$k]);
    }
}

没有测试,但我认为它应该工作......

于 2013-02-21T04:27:47.747 回答