我按照以下两个arrays
进行了跟踪,
$array1 = array
(
'Name' => 'Scott',
'Department' => 'Sales',
'Location' => 'USA',
'Details' => array
(
15 => '90%',
16 => '75%',
17 => '50%',
18 => '60%',
19 => '50%',
20 => '45%',
),
);
$array2 = array
(
'Action' => 'Imp',
'Name' => 'Scott',
'Department' => 'Sales',
'Location' => 'Canada',
'Details' => array
(
15 => '20%',
16 => '15%',
17 => '25%',
18 => '10%',
),
);
结果应该是:
$result_arr = array
(
'Action' => 'Imp',
'Name' => 'Scott',
'Department' => 'Sales',
'Location' => 'Canada',
'Details' => array
(
15 => '20%',
16 => '15%',
17 => '25%',
18 => '10%',
19 => '50%',
20 => '45%',
),
);
我已尝试array_merge
和array union (+) operator
,但这些都不符合我的要求。
答案 0 :(得分:1)
似乎结果数组应该看起来像$array2
而$array2['Details']
应该使用来自$array1['Details']
$result_arr = $array2;
$result_arr['Details'] += $array1['Details'];
答案 1 :(得分:1)
试试这个
$final= array_merge($array1,$array2);
$final['Details']= $final['Details']+$array1['Details'];
asort($final);
echo "<pre>"; print_r($final);
输出:
Array
(
[Location] => Canada
[Action] => Imp
[Department] => Sales
[Name] => Scott
[Details] => Array
(
[15] => 20%
[16] => 15%
[17] => 25%
[18] => 10%
[19] => 50%
[20] => 45%
)
)
答案 2 :(得分:1)
array_replace_recursive($array1, $array2);
答案 3 :(得分:0)
由于提供的信息很少,我可以使用此功能获得结果:
$array = array_merge($array1, $array2);
$array['Details'] = $array2['Details'] + $array1['Details'];
print_r($array);
结果:
Array
(
[Name] => Scott
[Department] => Sales
[Location] => Canada
[Details] => Array
(
[15] => 20%
[16] => 15%
[17] => 25%
[18] => 10%
[19] => 50%
[20] => 45%
)
[Action] => Imp
)
答案 4 :(得分:0)
$array2['Details'] = array_merge($array1['Details'], $array2['Details']);
$result = $array2;