/    Sign up×
Community /Pin to ProfileBookmark

Sort Merged Array by latest PubDate

Hello, I’m hoping someone can help me..I’m working on something that aggregates several RSS feeds and displays them as one list of news, unfortunately I have tried a couple of different methods of ordering the $pubDate ascending, and wondered if anyone had any ideas of how to order by pubDate?

[CODE]<?php

$formal = array();

…….

class Feed_Amalgamator
{
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 amalgamate()
{
shuffle( $this->data );
$temp = array();
foreach ( $this->data as $item )
{
if ( !in_array($item->link, $this->links($temp)) )
{
$temp[] = $item;
}
}
$this->data = $temp;
shuffle( $this->data );
}

private function links( array $items )
{
$links = array();
foreach ( $items as $item )
{
$links[] = $item->link;
}
return $links;
}
}

//this is where the arrays are constructed
try
{
$feeds = new Feed_Amalgamator;
$feeds->addFeeds( $formal );
$feeds->grabRss();
$feeds->amalgamate();
}
catch ( exception $e )
{
die( $e->getMessage() );
}

foreach ( $feeds->data as $item ) :
extract( (array) $item );
?>
<h2 class=”ui-bar-a”><a href=”<?php echo $link; ?>”><?php echo $title; ?></a></h2>
<p><?php echo $description; ?></p>
<p><em><?php echo $pubDate; ?></em></p>
<p><?php endforeach; ?></p>
[/CODE]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @LCrane86 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...