I'm trying to merge two arrays where the main key matches, I tried using array_merge but the key is just overwritten.
我正在尝试合并主键匹配的两个数组,我尝试使用array_merge但是键被覆盖了。
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 个解决方案
#1
5
In this case you can use simple array_merge_recursive
:
在这种情况下,您可以使用简单的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>';
#2
1
You should merge with respect to date ($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
工作演示:https://eval.in/839408
#3
1
Are you expecting something like this?
你期待这样的事吗?
在此处尝试此代码段
<?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);
#4
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,
),
)
#1
5
In this case you can use simple array_merge_recursive
:
在这种情况下,您可以使用简单的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>';
#2
1
You should merge with respect to date ($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
工作演示:https://eval.in/839408
#3
1
Are you expecting something like this?
你期待这样的事吗?
在此处尝试此代码段
<?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);
#4
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,
),
)