如何基于第一维键合并多个二维数组?

时间:2014-12-22 14:55:21

标签: php arrays

我的问题陈述如下: 假设我有2个二维数组。数组是:

$array1 = Array
(
[8] => Array
    (

        [branch_code] => DG-52484
        [total_desg] => 11

    )
);

$array2 = Array
(
[8] => Array
    (
        [total_dak] => 0
        [total_dak_decision] => 0

    )
);

在合并两个数组后,我需要的输出将是:

Array
(
[8] => Array
    (

        [branch_code] => DG-52484
        [total_desg] => 11
        [total_dak] => 0
        [total_dak_decision] => 0


    )
);

这种类型的任务是否有任何PHP功能。请注意,我对使用foreach或在我的情况下不感兴趣。

提前致谢。

3 个答案:

答案 0 :(得分:3)

它适用于array_replace_recursive

$array1 = Array(
    8 => Array(
        'branch_code' => 'DG-52484',
        'total_desg' => '11',
    )
);

$array2 = Array
(
    8 => Array(
        'total_dak' => 0,
        'total_dak_decision' => 0,
    )
);


var_dump(array_replace_recursive($array1, $array2));

输出

array (size=1)
  8 => 
    array (size=4)
      'branch_code' => string 'DG-52484' (length=8)
      'total_desg' => string '11' (length=2)
      'total_dak' => int 0
      'total_dak_decision' => int 0

答案 1 :(得分:0)

尝试使用

$array = array(
    8 => array_merge($array1[8],$array2[8]);
);

答案 2 :(得分:0)

您可以使用array_merge

$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);

输出

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

了解更多信息http://php.net/manual/tr/function.array-merge.php