title: PHP order multiple RSS feeds by date
body: <p>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:</p>
  
  <ol>
  <li><p>The two feeds get sorted separately (1st feed listed first, then
  second feed)</p></li>
  <li><p>The first feed gets sorted ascending and the second gets sorted descending.</p></li>
  </ol>
  
  <p>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!</p>
  
  <p>This is my code (<code>usort</code> and <code>foreach</code> loop at the bottom):</p>
  
  <pre><code>&lt;?php
  
  class Feed_Miro
  {
      public $urls = array();
      public $data = array();
  
      public function addFeeds( array $feeds )
      {
          $this-&gt;urls = array_merge( $this-&gt;urls, array_values($feeds) );
      }
  
      public function grabRss()
      {
          foreach ( $this-&gt;urls as $feed )
          {
              $data = @new SimpleXMLElement( $feed, 0, true );
              if ( !$data )
                  throw new Exception( 'Could not load: ' . $feed );
              foreach ( $data-&gt;channel-&gt;item as $item )
              {
                  $this-&gt;data[] = $item;
              }
          }
      }
  
      public function merge_feeds(){
  
          $temp = array();        
          foreach ( $this-&gt;data as $item )
          {
              if ( !in_array($item-&gt;link, $this-&gt;links($temp)) )
              {
                  $temp[] = $item;
              }
          }    $this-&gt;data = $temp;
  
      }
  
      private function links( array $items ){
          $links = array();
          foreach ( $items as $item )
          {
              $links[] = $item-&gt;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-&gt;addFeeds( $urls );
      $feeds-&gt;grabRss();
      $feeds-&gt;merge_feeds();
  }
  catch ( exception $e )
  {
      die( $e-&gt;getMessage() );
  }
  // test from here
  $data = array();
  $data = $feeds -&gt; data;
  
  usort($data, function($a,$b){
      return $a-&gt;pubDate - $b-&gt;pubDate;
  });
  
  foreach ( $data as $item ) :
  //foreach ( $feeds-&gt;data as $item ) :
  extract( (array) $item );
  ?&gt;
  &lt;div class="span4 profile"&gt;
  &lt;h3 class="profile-description"&gt;&lt;a href="&lt;?php echo $link; ?&gt;" style="text-decoration:none;"&gt;&lt;?php echo $title; ?&gt;&lt;/a&gt;&lt;/h3&gt;
  &lt;p class="profile-description"&gt;&lt;?php echo $description; ?&gt;&lt;br&gt;
  published: &lt;?php echo $pubDate; ?&gt; &lt;a href="&lt;?php echo $link ?&gt;" target="_blank"&gt;more&lt;/a&gt;&lt;/p&gt;
  &lt;/div&gt;
  
  &lt;?php endforeach; ?&gt;
  </code></pre>