0

views.php

<?php
    $events = $events->as_array();
    asort($events);
    foreach($events as $event):   
        // some code comes here
    endforeach;
?>

Above code is used to sort the array in ascending order.I want to sort the array value in ascending order by referring the value in from_time colunm in database,that is the value should order in ascending order with respect to the value in from_time column.The above i tried is sorting the value with respect to id.How to sort it with respect to field(from_time) in database.

Update:

$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'); 

I am getting this error "ErrorException [ Recoverable Error ]: Argument 1 passed to cmp() must be an array, object given" in line "function cmp(array $a, array $b) {".

Thanks

user2681579
  • 1,333
  • 1
  • 22
  • 43
  • 1
    http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php/17364128#17364128 – m1kfb Mar 20 '14 at 12:00

1 Answers1

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);
?>
Re Captcha
  • 3,101
  • 2
  • 19
  • 33
prasoon
  • 733
  • 6
  • 19
  • 4
    It would be useful to provide some explanation of what you are doing. Also, please tidy up your indentation! – Tom Fenech Mar 20 '14 at 12:16