简体   繁体   中英

PHP merge two arrays where keys match

I'm trying to merge two arrays where the main key matches, I tried using array_merge but the key is just overwritten.

For example I have this array:

$date = '2017-08-01';

$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
print_r($price_arr_1);

Which outputs:

Array ( [2017-08-01] => Array ( [adult_1] => 10 [child_1] => 2 ) ) 

And I have this array:

$date = '2017-08-01';

$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
print_r($price_arr_2);

Which outputs:

Array ( [2017-08-01] => Array ( [adult_2] => 10 [child_2] => 2 ) ) 

When I try and merge them like this:

print_r(array_merge($price_arr_1,$price_arr_2));

It output this:

Array ( [2017-08-01] => Array ( [adult_2] => 10 [child_2] => 2 ) ) 

I want to output this:

Array ( [2017-08-01] => Array ( [adult_1] => 10 [adult_2] => 10 [child_1] => 2 [child_2] => 2 ) ) 

Appreciated any ideas as to how to achieve the above!

In this case you can use simple array_merge_recursive :

$a1 = Array ( '2017-08-01' => Array ( 'adult_1' => 10, 'child_1' => 2, ) );
$a2 = Array ( '2017-08-01' => Array ( 'adult_2' => 20, 'child_2' => 4, ) );

echo'<pre>',print_r(array_merge_recursive($a1, $a2)),'</pre>';

You should merge with respect to date ( $date ):

<?php

$date = '2017-08-01';

$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;
print_r($price_arr_1);

$date = '2017-08-01';

$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;
print_r($price_arr_2);

print_r(array_merge($price_arr_1[$date],$price_arr_2[$date]));

Here is the output:

Array
(
    [2017-08-01] => Array
        (
            [adult_1] => 10
            [child_1] => 2
        )

)
Array
(
    [2017-08-01] => Array
        (
            [adult_2] => 10
            [child_2] => 2
        )

)
Array
(
    [adult_1] => 10
    [child_1] => 2
    [adult_2] => 10
    [child_2] => 2
)

Working demo: https://eval.in/839408

Are you expecting something like this?

Try this code snippet here

<?php

ini_set('display_errors', 1);

$date = '2017-08-01';
$price_arr_1 = array();
$price_arr_1[$date]['adult_1'] = 10;
$price_arr_1[$date]['child_1'] = 2;

$date = '2017-08-01';    
$price_arr_2 = array();
$price_arr_2[$date]['adult_2'] = 10;
$price_arr_2[$date]['child_2'] = 2;

foreach($price_arr_1 as $someDate => $data)
{
    if(isset($price_arr_2[$someDate]))
    {
        $price_arr_1[$someDate]=array_merge($price_arr_1[$someDate],$price_arr_2[$someDate]);
    }
    else
    {
        $price_arr_1[$someDate]=$price_arr_2[$someDate];
    }
}
print_r($price_arr_1);

Better than putting a bandaid on your code, I will urge you to change your approach entirely. Most simply, avoid using any array functions at all. Just build the result array as you declare each set of elements. Improve your code's performance like this:

Code: ( Demo )

$date = '2017-08-01';

$prices[$date]=['adult_1'=>10,'child_1'=>2];
$prices[$date]+=['adult_2'=>10,'child_2'=>2];  // notice the + sign

ksort($prices[$date]);  // optionally, you can sort the subarrays ASC by key name
var_export($prices);

Output:

array (
  '2017-08-01' => 
  array (
    'adult_1' => 10,
    'adult_2' => 10,
    'child_1' => 2,
    'child_2' => 2,
  ),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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