[英]Need help sorting multi array
我在這里看到了很多示例,但是對我來說不起作用。
我有一個從數據庫中檢索到的已排序事件的列表:
16.08.2010 12:00:00
: 21.08.2010 20:00:00
16.08.2010 20:00:00
: 21.08.2010 23:00:00
18.08.2010 17:00:00
: 18.08.2010 19:00:00
如您所見,第一個事件是從16.08到21.08。
我需要“切碎”,以便每天獲得一份。
這是我的功能:
function fullList( $data ) {
$ev = $data['events'];
$events = array();
// Loop through each event. If event spans over several days, add each day in to new event list
foreach($ev as $e) :
$endDate = strtotime(date('Y-m-d 23:59:59', strtotime($e->endTime)));
$current = strtotime($e->startTime);
// Copy event so data is not overwritten
$newEv = $e;
while ($current <= $endDate) {
//Set start date of event to new date
$newEv->startTime = date('d.m.Y H:i:s', $current);
// Add events to new event list
array_push($events,$newEv);
//Go to next date
$current = strtotime('+1 day', $current);
}
endforeach;
// Need to sort $events here
}
現在, $events
包含所有事件,但未按日期排序。 我已經嘗試過uasort ,但是不能使用uasort($array, 'cmp');
。
我該如何按日期對該數組進行排序?
您是否嘗試過自然排序算法?
正如Nazariy所述,最好從源頭對事件進行預排序(假設事件來自數據庫)。
如果必須使用代碼對數組進行排序,則應該可以使用usort。 我假設您要按startTime
排序
usort($events, function($a, $b) {
return strtotime($a->startTime) - strtotime($b->startTime);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.