我在这里看到很多例子,但它对我不起作用 .
我有一个从我的DB检索到的已排序事件列表:
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 .
我需要"chop this up"才能在前一天获得一个条目 .
这是我的功能:
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
包含所有事件,但它's not sorted by date. I'已尝试uasort,但我不能使用 uasort($array, 'cmp');
.
如何按日期排序此数组?
2 回答
你试过natural sorting算法吗?
正如Nazariy所提到的,你最好在源头预先排序事件(假设它们来自数据库) .
如果必须在代码中对数组进行排序,则应该能够使用usort . 我假设你要按
startTime
排序