简体   繁体   English

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

[英]How to merge multiple two dimensional array based on first dimension key?

My problem statement is like follows: Suppose I have 2 two dimensional array. 我的问题陈述如下:假设我有2个二维数组。 The arrays are: 数组是:

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

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

    )
);

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

    )
);

After combining the two array my required out put will be: 合并两个数组后,我所需的输出将是:

Array
(
[8] => Array
    (

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


    )
);

Is there any php function for this type of task. 是否有用于此类任务的任何php函数。 Please note that i am not interested to use foreach or while in my situation. 请注意,我对使用foreach或在我的情况下不感兴趣。

Thanks in advance. 提前致谢。

It will work with array_replace_recursive : 它将与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));

Output 输出量

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

try to use 尝试使用

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

You can use array_merge 您可以使用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);

Output 输出量

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

For more info http://php.net/manual/tr/function.array-merge.php 有关更多信息,请访问http://php.net/manual/tr/function.array-merge.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM