note 37664 modified in function.usort by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 16:30:22 +0000 |
| Subject: | note 37664 modified in function.usort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176079@lists.php.net to get a copy of this message | ||
I have a class with a bunch of functions that work with an SQL database. I am working with calendar
dates that occur more than once (like once a week for 2 months), so I have to generate the date in
php after the SQL. But I wanted to sort the events based on the date, so I tried using usort, but
was unable to get it to work, because it didn't think that my sorting function was defined
(even though it was in the same class). I have a separate class to store the data, that has a
variable called Start which is a PHP date object.
So here is how I got it to work. First I created a temporary function, that returned the value of a
string comparison of the timestamps for the dates. Then I used that temporary function for the
usort. Enjoy.
<?php
$tempfunction = create_function('$event1,$event2',
'return strcmp($event1->Start[0],$event2->Start[0]);');
usort($data,$tempfunction);
?>
--was--
I have a class with a bunch of functions that work with an SQL database. I am working with calendar
dates that occur more than once (like once a week for 2 months), so I have to generate the date in
php after the SQL. But I wanted to sort the events based on the date, so I tried using usort, but
was unable to get it to work, because it didn't think that my sorting function was defined
(even though it was in the same class). I have a separate class to store the data, that has a
variable called Start which is a PHP date object.
So here is how I got it to work. First I created a temporary function, that returned the value of a
string comparison of the timestamps for the dates. Then I used that temporary function for the
usort. Enjoy.
$tempfunction = create_function('$event1,$event2',
'return strcmp($event1->Start[0],$event2->Start[0]);');
usort($data,$tempfunction);
http://php.net/manual/en/function.usort.php