note 37664 added to function.usort
| From: | kristen at ccshoreline dot org | Date: | Sat, 22 Nov 2003 05:39:13 +0000 |
| Subject: | note 37664 added to function.usort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-60806@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.
$tempfunction = create_function('$event1,$event2',
'return strcmp($event1->Start[0],$event2->Start[0]);');
usort($data,$tempfunction);
----
Manual Page -- http://www.php.net/manual/en/function.usort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37664
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37664&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37664&report=yes
Search -- http://master.php.net/manage/user-notes.php