Johan
Johan

Reputation: 211

sort rss.xml by pubdate

Is there a way I can sort this RSSfeed by pubDate in descending order? I tried sort and some other stuff but I can't get it to work. I want to show the 5 last modified pages.

<?php
$rss = new DOMDocument();
$rss->load('../../rssfeed.xml');
$feed = array();

foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array(
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    );
    array_push($feed, $item);
}
$limit = 5;

for ($x = 0; $x < $limit; $x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="' . $link . '" title="' . $title . '">' . $title . '</a></strong><br />';
    echo '<small><em>Posted on ' . $date . '</em></small></p>';
    echo '<p>' . $description . '</p>';
}
?>

Upvotes: 0

Views: 2772

Answers (3)

Chris Wheeler
Chris Wheeler

Reputation: 1736

A more compact version of DaSourcerer's answer using Anonymous functions and the Spaceship operator (Requires PHP7):

usort($feed, function($a, $b) {
    return strtotime($a['date']) <=> strtotime($b['date']);
});

Of if you want them sorted with the newest first just swap $a and $b within the function:

usort($feed, function($a, $b) {
    return strtotime($b['date']) <=> strtotime($a['date']);
});

Upvotes: 0

Tim Groeneveld
Tim Groeneveld

Reputation: 9049

Here is some code that uses array_multisort to sort the times in the RSS feed. Notice the changes to the $item array that is being made.

<?php
$rss = new DOMDocument();
$rss->load('../../rssfeed.xml');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array (
            'title' => htmlspecialchars_decode($node->getElementsByTagName('title')->item(0)->nodeValue),
            'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'time' => strtotime($node->getElementsByTagName('pubDate')->item(0)->nodeValue),
            );
    $x++;
    $times[$x] = $item['time'];
    $items[$x] = $item;
}

array_multisort($times, SORT_DESC, $items);
$items = array_slice( $items, 5 );

foreach($items as $item) {
    $date = date('l F d, Y', strtotime($item['date']));
    echo '<p><strong><a href="'.$item['link'].'" title="'.$item['title'].'">'.htmlspecialchars($item['title']).'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p>'.$item['description'].'</p>';
}

Upvotes: 0

DaSourcerer
DaSourcerer

Reputation: 6606

You can use usort() on your feed array. Using strtotime() on $item['data'] as items' compare value should get you set:

function compareItems($a,$b) {
    $a=strtotime($a['date']);
    $b=strtotime($b['date']);

    if($a==$b)
        return 0;
    elseif($a < $b)
        return -1;
    else
        return 1;
}

Now everything left to do is to run usort($feed,'compareItems');

Upvotes: 2

Related Questions