PHP - 推送数组关联到另一个数组关联

时间:2017-05-20 18:36:04

标签: php arrays

我有两个数组,两个数组的长度相同。 让我们说第一个数组有树元素, 第二个数组也有三个元素。 这是第一个阵列。

paste()

第二个数组是:

Array
(
[0] => Array
    (
        [container] => SITU9026744
        [seal] => SITC853014
    )

[1] => Array
    (
        [container] => SITU9026744
        [seal] => SITC853014
    )

[2] => Array
    (
        [container] => SITU9026744
        [seal] => SITC853014
    )
)

然后,我需要这样:

Array
(
[0] => Array
    (
        [size] => 40x80x1.15x6M
        [weight] => 12800
        [piece_per_bundle] => 50
        [total_bundle] => 20
        [total_piece] => 1000
        [total_quantity_(MTs)] => 12800
        [tanggal_plan_masuk] => 01-05-2017
        [nama_file] => PACKING LIST 68 LOT 1 WITH COLORS MARK.xlsx
        [urut] => 1
        [row] => 34
    )

[1] => Array
    (
        [size] => 40x60x1.15x6M
        [weight] => 10630
        [piece_per_bundle] => 50
        [total_bundle] => 10
        [total_piece] => 500
        [total_quantity_(MTs)] => 5315
        [tanggal_plan_masuk] => 01-05-2017
        [nama_file] => PACKING LIST 68 LOT 1 WITH COLORS MARK.xlsx
        [urut] => 2
        [row] => 35
    )

[2] => Array
    (
        [size] => 19x19x0.92x6M
        [weight] => 3160
        [piece_per_bundle] => 100
        [total_bundle] => 12
        [total_piece] => 1200
        [total_quantity_(MTs)] => 3792
        [tanggal_plan_masuk] => 01-05-2017
        [nama_file] => PACKING LIST 68 LOT 1 WITH COLORS MARK.xlsx
        [urut] => 3
        [row] => 36
    )
)

我们可以使用什么键来推送第一个键=>值数组到第二个键=>值。

请告知。

1 个答案:

答案 0 :(得分:1)

您可能需要array_replace_recursive功能:

<?php
$ar1 = [
    [
        'a' => 1,
        'b' => 2
    ],
    [
        'a' => 3,
        'b' => 4
    ]
];
$ar2 = [
    [
        'c' => 15
    ],
    [
        'c' => 16
    ]
];

$res = array_replace_recursive($ar1, $ar2);
var_dump($res);

输出将是:

array(2) {
  [0]=>
  array(3) {
    ["a"]=>
    int(1)
    ["b"]=>
    int(2)
    ["c"]=>
    int(15)
  }
  [1]=>
  array(3) {
    ["a"]=>
    int(3)
    ["b"]=>
    int(4)
    ["c"]=>
    int(16)
  }
}

这是一个有效的例子:
http://sandbox.onlinephpfunctions.com/code/fec68f6c555aa8bd1dcbc471ef95172cf5a1e9be

相关问题