Kishore Reddy
Kishore Reddy

Reputation: 157

Merge two multidimensional arrays with same key pair

I have two multidimensional arrays with same number of keys as below

Array
(

[2012035] => Array
    (
        [first_name] => ABC
        [last_name] => DEF
    )

[2012045] => Array
    (
        [first_name] => GHI
        [last_name] => JKL
    )

[2012046] => Array
    (
        [first_name] => MNO
        [last_name] => PQR
    )

[2012050] => Array
    (
        [first_name] => STU
        [last_name] => VWX
    )
)

and

Array
(

[2012035] => Array
    (
        [01-07-2015] => 123 
        [02-07-2015] => 456 
    )

[2012045] => Array
    (
        [01-07-2015] => 789 
        [02-07-2015] => 101112 
    )

[2012046] => Array
    (
        [01-07-2015] => 131415 
        [02-07-2015] => 161718 
    )

[2012050] => Array
    (
        [01-07-2015] => 192021 
        [02-07-2015] => 222324
    )
)

Now I want the output as below

Array
(
[2012035] => Array
    (
        [first_name] => ABC
        [last_name] => DEF
        [01-07-2015] => 123 
        [02-07-2015] => 456 
    )

[2012045] => Array
    (
        [first_name] => GHI
        [last_name] => JKL
        [01-07-2015] => 789 
        [02-07-2015] => 101112
    )

[2012046] => Array
    (
        [first_name] => MNO
        [last_name] => PQR
        [01-07-2015] => 131415 
        [02-07-2015] => 161718
    )

[2012050] => Array
    (
        [first_name] => STU
        [last_name] => VWX
        [01-07-2015] => 192021 
        [02-07-2015] => 222324
    )
)

I am able to do it using foreach in PHP .

But is there any built in PHP function that can do this work with out using foreach?

Upvotes: 4

Views: 2146

Answers (1)

lvil
lvil

Reputation: 4326

I think this can work for you:

$arr_1 = array(333 => array('a1' => 1));
$arr_2 = array(333 => array('a2' => 2), 444 => array('a3' => 3));

$arr_res = array_replace_recursive($arr_1, $arr_2);
var_dump($arr_res);

Upvotes: 7

Related Questions