如何通过键合并两个多维数组?

时间:2019-06-28 15:35:37

标签: php arrays

我有两个具有相同键的多维数组。我想将这些数组合并为一个。

$sourceData = $array1;
$sourceDataN = $array2;

foreach ($sourceData as $keySd => $sd){

        foreach ($sourceDataN as $keySdn => $sdn){
            if($keySd == $keySdn){
                foreach ($sd as $keySdd => $sdd  ){
                    foreach ($sdn as $keySdnn => $sdnn  ){
                        if($keySdd == $keySdnn){
                           $new[$keySd][] = array_merge($sdd, $sdnn);
                        }
                    }
                }
            }
        }
    }


Array (
  [809] => Array
    (
        [ss] => Array
            (
                [qty] => 0
            )

        [bh] => Array
            (
                [qty] => 14
            )

        [li] => Array
            (
                [qty] => 0
            )

        [mg] => Array
            (
                [qty] => 0
            )

    )

  [1944] => Array
    (
        [ss] => Array
            (
                [qty] => 25
            )

        [bh] => Array
            (
                [qty] => 10
            )

        [za] => Array
            (
                [qty] => 10
            )

        [li] => Array
            (
                [qty] => 39
            )

        [mg] => Array
            (
                [qty] => 0
            )

    )

)
Array (
  [809] => Array
    (
        [ss] => Array
            (
                [price] => 603.07
            )

        [bh] => Array
            (
                [price] => 649.95
            )

        [li] => Array
            (
                [price] => 628.48
            )

        [mg] => Array
            (
                [price] => 629
            )

    )

  [1944] => Array
    (
        [ss] => Array
            (
                [price] => 848.7
            )

        [bh] => Array
            (
                [price] => 883.74
            )

        [za] => Array
            (
                [price] => 847.65
            )

        [li] => Array
            (
                [price] => 866.02
            )

        [mg] => Array
            (
                [price] => 847.59
            )

    )

)


Array (
  [809] => Array
    (
        [ss] => Array
            (
                [price] => 603.07
                [qty] => 0
            )

        [bh] => Array
            (
                [price] => 649.95
                [qty] => 14
            )

        [li] => Array
            (
                [price] => 628.48
                [qty] => 0
            )

        [mg] => Array
            (
                [price] => 629
                [qty] => 0
            )

    )

  [1944] => Array
    (
        [ss] => Array
            (
                [price] => 848.7
                [qty] => 25
            )

        [bh] => Array
            (
                [price] => 883.74
                [qty] => 10
            )

        [za] => Array
            (
                [price] => 847.65
                [qty] => 10
            )

        [li] => Array
            (
                [price] => 866.02
                [qty] => 39
            )

        [mg] => Array
            (
                [price] => 847.59
                [qty] => 0
            )

    )

)

1 个答案:

答案 0 :(得分:0)

您不需要全部循环-只需第一个数组,然后使用第二个的键即可。

您可以将简单的嵌套foreach循环用作:

$res = [];
foreach($array1 as $k => $v) { // $k here are the number
    foreach($v as $kk => $vv) { // inner arrays - $kk as 2 char
        $res[$k][$kk] = array_merge($array1[$k][$kk], $array2[$k][$kk]);
    }
}

实时示例:3v4l