这会将第一个唯一的工作日复制到新数组中,如果该工作日重复,则只会添加事件ID。
$events = [
[
\'week-day\' => \'tuesday\',
\'day-date\' => 1511841600,
\'events_ids\' => [83417]
],
[
\'week-day\' => \'wednesday\',
\'day-date\' => 1511913600,
\'events_ids\' => [83417]
],
[
\'week-day\' => \'thrusday\',
\'day-date\' => 1512000000,
\'events_ids\' => [83417]
],
[
\'week-day\' => \'tuesday\',
\'day-date\' => 1511830800,
\'events_ids\' => [83417]
]
];
$new_events = [];
foreach ( $events as $event ) {
if ( array_key_exists( $event[\'week-day\'], $new_events ) ) {
// If we have this week day just merge the event ids.
$new_events[$event[\'week-day\']][\'events_ids\'] = array_merge(
$new_events[$event[\'week-day\']][\'events_ids\'],
$event[\'events_ids\']
);
} else {
$new_events[$event[\'week-day\']] = $event;
}
}
$new_events = array_values($new_events);
如果初始数组中的事件不是仅来自一周的事件,则应关注
day-date
- 将所有事件ID与之关联,然后您可以轻松获得该唯一日期的工作日名称。这种方法将更加通用。