假设我有以下数组:
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]上合并这些数组。期望的输出排序应与第一个数组中的相同。
我已设法以不同的方式合并这些数组但失败了:
我怎样才能做到这一点?任何帮助深表感谢。
非常简单,只需循环第二个数组并使用键合并到第一个数组:
foreach($array2 as $key => $val) {
$array1[$key]['event_detail'] = array_merge($array1[$key]['event_detail'],
$val['event_detail']);
}