I have a bit of PHP code to merge two RSS feeds. I was trying to sort the feeds by date, but I get the funny result that:
The two feeds get sorted separately (1st feed listed first, thensecond feed)
The first feed gets sorted ascending and the second gets sorted descending.
I want ALL of the data (feed 1 + 2 together) to be sorted by date with the latest item first. Any help is much appreciated!
This is my code (usort
and foreach
loop at the bottom):
<?phpclass Feed_Miro{ public $urls = array(); public $data = array(); public function addFeeds( array $feeds ) { $this->urls = array_merge( $this->urls, array_values($feeds) ); } public function grabRss() { foreach ( $this->urls as $feed ) { $data = @new SimpleXMLElement( $feed, 0, true ); if ( !$data ) throw new Exception( 'Could not load: ' . $feed ); foreach ( $data->channel->item as $item ) { $this->data[] = $item; } } } public function merge_feeds(){ $temp = array(); foreach ( $this->data as $item ) { if ( !in_array($item->link, $this->links($temp)) ) { $temp[] = $item; } } $this->data = $temp; } private function links( array $items ){ $links = array(); foreach ( $items as $item ) { $links[] = $item->link; } return $links; }}/********* add urls*********/$urls = array( 'http://www.voyagersfamily.ch/feed.xml', 'http://www.miroman.ch/feed.xml' );try{ $feeds = new Feed_Miro; $feeds->addFeeds( $urls ); $feeds->grabRss(); $feeds->merge_feeds();}catch ( exception $e ){ die( $e->getMessage() );}// test from here$data = array();$data = $feeds -> data;usort($data, function($a,$b){ return $a->pubDate - $b->pubDate;});foreach ( $data as $item ) ://foreach ( $feeds->data as $item ) :extract( (array) $item );?><div class="span4 profile"><h3 class="profile-description"><a href="<?php echo $link; ?>" style="text-decoration:none;"><?php echo $title; ?></a></h3><p class="profile-description"><?php echo $description; ?><br>published: <?php echo $pubDate; ?> <a href="<?php echo $link ?>" target="_blank">more</a></p></div><?php endforeach; ?>