1

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!

4 Answers 4

6

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>';
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.