Fadly Dzil
Fadly Dzil

Reputation: 2206

PHP - Push Array Associative to Another Array associative

I have two array which the length both of array is same. Let's say that first arrays have tree elements, and the second array also three elements too. This is the first array.

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

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

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

The second array is :

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
    )
)

Then, I need like this :

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
        [container] => SITU9026744
        [seal] => SITC853014
    )

[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
        [container] => SITU9026744
        [seal] => SITC853014
    )

[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
        [container] => SITU9026744
        [seal] => SITC853014
    )
)

What key that we can use to push the first key => value array into second key => value.

Please advise.

Upvotes: 1

Views: 22

Answers (1)

Dekel
Dekel

Reputation: 62536

You would probably want the array_replace_recursive function:

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

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

output will be:

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)
  }
}

Here is a working example:
http://sandbox.onlinephpfunctions.com/code/fec68f6c555aa8bd1dcbc471ef95172cf5a1e9be

Upvotes: 1

Related Questions