note 94839 added to function.usort

From: Date: Fri, 27 Nov 2009 19:42:10 +0000
Subject: note 94839 added to function.usort
Groups: php.notes 
Request: Send a blank email to php-notes+get-163346@lists.php.net to get a copy of this message
For anyone else sorting mixed RSS feeds in date order, here's an example of the perhaps not so obvious way of calling a method from the same class using usort(): class MyAggregator { static function cmp_pubdate( $a, $b ) { $a_t = strtotime( $a['pubdate'] ) ; $b_t = strtotime( $b['pubdate'] ) ; if( $a_t == $b_t ) return 0 ; return ($a_t > $b_t ) ? -1 : 1; } // assemble arrays for display in date order function all_documents(){ $all_data = array_merge ( $this->upcoming(), $this->past() ) ; // Use within its own class using the $this syntax so: usort( $all_data, array( $this , "cmp_pubdate" ) ) ; return $all_data ; } } As previously said, use as a static function in other parts of your code: usort ( $a, array( "MyAggregator", "cmp_pubdate" ) ) ; ---- Server IP: 69.147.83.197 Probable Submitter: 90.60.94.93 ---- Manual Page -- http://www.php.net/manual/en/function.usort.php Edit -- https://master.php.net/note/edit/94839 Del: integrated -- https://master.php.net/note/delete/94839/integrated Del: useless -- https://master.php.net/note/delete/94839/useless Del: bad code -- https://master.php.net/note/delete/94839/bad+code Del: spam -- https://master.php.net/note/delete/94839/spam Del: non-english -- https://master.php.net/note/delete/94839/non-english Del: in docs -- https://master.php.net/note/delete/94839/in+docs Del: other reasons-- https://master.php.net/note/delete/94839 Reject -- https://master.php.net/note/reject/94839 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163346) next »