假设我有以下数组:
Array
(
[2001] => Array
(
[event_detail] => Array
(
[1] => Array
(
[event_name] => Advocacy CPD 2019
[cpd_points] => 23
)
[5] => Array
(
[event_name] => Advocacy CPD 2019 new
[cpd_points] => 2
)
)
)
[2002] => Array
(
[event_detail] => Array
(
[2] => Array
(
[event_name] => Advocacy CPD 2011
[cpd_points] => 28
)
[4] => Array
(
[event_name] => Advocacy CPD 20195 new
[cpd_points] => 12
)
)
)
)
第二个:
Array
(
[2001] => Array
(
[event_detail] => Array
(
[1] => Array
(
[event_name] => Advocacy CPD 2020
[cpd_points] => 27
)
)
)
[2052] => Array
(
[event_detail] => Array
(
[2] => Array
(
[event_name] => Advocacy CPD 2052
[cpd_points] => 258
)
[4] => Array
(
[event_name] => Advocacy CPD 2019445 new
[cpd_points] => 78
)
)
)
)
所需的输出是:
Array
(
[2001] => Array
(
[event_detail] => Array
(
[1] => Array
(
[event_name] => Advocacy CPD 2019
[cpd_points] => 23
)
[5] => Array
(
[event_name] => Advocacy CPD 2019 new
[cpd_points] => 2
)
[6] => Array
(
[event_name] => Advocacy CPD 2020
[cpd_points] => 27
)
)
)
[2002] => Array
(
[event_detail] => Array
(
[2] => Array
(
[event_name] => Advocacy CPD 2011
[cpd_points] => 28
)
[4] => Array
(
[event_name] => Advocacy CPD 20195 new
[cpd_points] => 12
)
)
)
[2052] => Array
(
[event_detail] => Array
(
[2] => Array
(
[event_name] => Advocacy CPD 2052
[cpd_points] => 258
)
[4] => Array
(
[event_name] => Advocacy CPD 2019445 new
[cpd_points] => 78
)
)
)
)
问题是我想在相同的id [2001,2005,2002]上合并这些数组。所需的输出排序应与第一个数组中的排序相同。
我设法用不同的方式合并了这些数组,但是失败了:
我该如何实现?非常感谢您的帮助。
答案 0 :(得分:1)
非常简单,只需循环第二个数组并使用第一个数组的键合并即可:
foreach($array2 as $key => $val) {
$array1[$key]['event_detail'] = array_merge($array1[$key]['event_detail'],
$val['event_detail']);
}