【问题标题】:PHP order multiple RSS feeds by datePHP 按日期订购多个 RSS 提要
【发布时间】:2017-03-22 16:53:26
【问题描述】:

我有一些 PHP 代码来合并两个 RSS 提要。我试图按日期对提要进行排序,但我得到了一个有趣的结果:

  1. 两个提要分别排序(第一个提要先列出,然后 第二个饲料)

  2. 第一个提要按升序排序,第二个提要按降序排序。

我希望所有数据(将 1 + 2 一起提供)按日期排序,最新项目首先。非常感谢任何帮助!

这是我的代码(usortforeach 在底部循环):

<?php

class 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; ?>

【问题讨论】:

    标签: php sorting rss


    【解决方案1】:

    您正在比较字符串,而您需要比较时间戳

    这部分:

    usort($data, function($a,$b){
        return $a->pubDate - $b->pubDate;
    });
    

    需要转换为时间戳并进行如下比较:

    usort($data, function($a,$b){
        return strtotime($a->pubDate) - strtotime($b->pubDate);
    });
    

    例如:

    $arr = [
        ['pubDate' => 'Wed, 07 Nov 2012 10:15:00 GMT',],
        ['pubDate' => 'Thu, 23 Feb 2017 09:00:00 GMT', ],
        ['pubDate' => 'Mon, 30 May 2016 10:00:00 GMT', ]
    ];
    
    usort($arr, function($a, $b) {
        return strtotime($a['pubDate']) > strtotime($b['pubDate']);
    });
    
    print_r($arr);
    

    这将返回:

    Array (
        [0] => Array (
            [pubDate] => Wed, 07 Nov 2012 10:15:00 GMT
        )
        [1] => Array (
            [pubDate] => Mon, 30 May 2016 10:00:00 GMT
        )
        [2] => Array (
            [pubDate] => Thu, 23 Feb 2017 09:00:00 GMT
        )
    )
    

    在对数组进行排序而不转换为时间戳时不会对其进行排序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2023-03-13
      相关资源
      最近更新 更多