views.php
<?php
$events = $events->as_array();
asort($events);
foreach($events as $event):
// some code comes here
endforeach;
?>
上面的代码用于按升序对数组进行排序。我想通过引用数据库中from_time
colunm中的值按升序对数组值进行排序,即值应按升序顺序排序到from_time
列中的值。上面我尝试的是对id进行排序。如何根据数据库中的field(from_time)
对其进行排序。
更新
$events = $events->as_array();
function cmp(array $a, array $b) {
if ($a['event_from_time'] < $b['event_from_time']) {//event_from_time is the db column name
return -1;
} else if ($a['event_from_time'] > $b['event_from_time']) {
return 1;
} else {
return 0;
}
}
usort($events, 'cmp');
我收到此错误&#34; ErrorException [ Recoverable Error ]: Argument 1 passed to cmp() must be an array, object given
&#34;排队&#34; function cmp(array $a, array $b) {
&#34;。
由于
答案 0 :(得分:0)
<?php
$events = $events->as_array();
foreach($events as $event):
$res_array[$event->mycat][] = $event->from_time;
endforeach;
ksort($res_array);
print_r($res_array);
?>