如何将SimpleXML中的多个RSS源放入按pubDate排序的数组中?
例:
Feed[0] = 'http://www.example.org/Feed1.RSS';
Feed[1] = 'http://www.thing.org/Feed.RSS';
...
Feed[n] = '..';
#Fetch Feeds
#Sort by pubDate
foreach ($Feeds as $row) {
//Do something
print '<item>
<title>...</title>
</item>';
}
解决方法:
// Set the Feed URLs here
$Feeds = array(
'http://www.example.org/Feed1.RSS',
'http://www.example.org/Feed2.RSS',
// etc.
);
// Get all Feed entries
$entries = array();
foreach ($Feeds as $Feed) {
$xml = simplexml_load_file($Feed);
$entries = array_merge($entries, $xml->xpath('/RSS//item'));
}
// Sort Feed entries by pubDate (ascending)
usort($entries, function ($x, $y) {
return strtotime($x->pubDate) - strtotime($y->pubDate);
});
print_r($entries);
适用于PHP 5.3.