note 94839 modified in function.usort by danbrown

From: Date: Fri, 27 Nov 2009 19:44:01 +0000
Subject: note 94839 modified in function.usort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163347@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(): <?php 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: <?php usort ( $a, array( "MyAggregator", "cmp_pubdate" ) ) ; ?> --was-- 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" ) ) ; http://php.net/manual/en/function.usort.php

« previous php.notes (#163347) next »