有没有办法按降序按pubdate排序

时间:2013-12-18 15:42:29

标签: php xml

我在php文件中读取了我的rss.xml文件。 现在我想按降序的pubdate顺序对rss feed进行排序。这可能吗

<?php
$rss = simplexml_load_file('../../rssfeed.xml');
echo '<h1>'. $rss->channel->title . '</h1>';
foreach ($rss->channel->item as $item) {
   echo '<h2><a href="'. $item->link .'">' . $item->title . "</a></h2>";
   echo "<p>" . $item->pubDate . "</p>";
   echo "<p>" . $item->description . "</p>";
} 
?>

我在考虑usort,但我无法弄清楚如何让它发挥作用......

要测试的xml rss代码

<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
        <title>My RSS feed</title>

<item>
            <title>01Home</title>
<description>De hoofdpagina van flux cms. Op deze pagina worden de features en plugins van flux cms weergegeven. Flux cms is enorm aanpasbaar zodat je je website kan maken zoals jij dat wil. Zo kan je bvb een eigen teplate aanmaken zodat je website volledig aan jou verwachtingen voldoet.</description>
<link>http://localhost/index.php?p=01Home</link>
            <pubDate>2013 12 17</pubDate>
                </item><item>
            <title>02Gastenboek</title>
<description>Via het gastenboek kan je je bezoekers een bericht laten schrijven op je website dat leesbaar is voor alle andere sitegebruikers.</description>
<link>http://localhost/index.php?p=02Gastenboek</link>
            <pubDate>2013 12 05</pubDate>
                </item><item>
            <title>03Albums</title>
<description>Via de albums plugin heb je de mogelijkheid om 5 albums aan te maken en weer te geven. Via een makelijk upload en verwijder systeem kan je afbeeldingen toevoegen en weer verwijderen. De albums zijn aanklikbaar en worden in een ooie slider weergegeven via een pop up.</description>
<link>http://localhost/index.php?p=03Albums</link>
            <pubDate>2013 12 18</pubDate>
                </item><item>
            <title>04Videos</title>
<description>videos.</description>
<link>http://localhost/index.php?p=04Videos</link>
            <pubDate>2013 12 18</pubDate>
                </item><item>
            <title>05Poll</title>
<description>Via deze plugin kan je makkelijk een poll toevoegen ergens op je website. Je kan een hoofdvraag stellen en max 4 mogelijke aantwoorden aanklikken. Handig om bvb te weten wat gebruikers van je site vinden.</description>
<link>http://localhost/index.php?p=05Poll</link>
            <pubDate>2013 12 05</pubDate>
                </item><item>
            <title>06Events</title>
<description>Events van de kalender</description>
<link>http://localhost/index.php?p=06Events</link>
            <pubDate>2013 12 17</pubDate>
                </item><item>
            <title>07Nieuwsbrief</title>
<description>Nieuwsbrief.</description>
<link>http://localhost/index.php?p=07Nieuwsbrief</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>08Facebook</title>
<description>facebook</description>
<link>http://localhost/index.php?p=08Facebook</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>09Zoeken</title>
<description>zoeken.</description>
<link>http://localhost/index.php?p=09Zoeken</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>10statistieken</title>
<description>statistieken.</description>
<link>http://localhost/index.php?p=10statistieken</link>
            <pubDate>2013 12 17</pubDate>
                </item><item>
            <title>11Profielen</title>
<description>profielen.</description>
<link>http://localhost/index.php?p=11Profielen</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>12Privebericht</title>
<description>Plugin waarbij de sitegebruiker een bericht kan versturen naar de webbeheerder.</description>
<link>http://localhost/index.php?p=12Privebericht</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>13Blog</title>
<description>blog.</description>
<link>http://localhost/index.php?p=13Blog</link>
            <pubDate>2013 11 12</pubDate>
                </item><item>
            <title>14Kalender</title>
<description>kalender.</description>
<link>http://localhost/index.php?p=14Kalender</link>
            <pubDate>2013 12 10</pubDate>
                </item><item>
            <title>15Search</title>
<description>Met deze functie kan je intern alle paginas zoeken. De zoekfunctie zoekt op de paginabeschrijvingswoorden die je opgeeft bij het creeren en bewerken van de pagina's.</description>
<link>http://localhost/index.php?p=15Search</link>
            <pubDate>2013 12 04</pubDate>
                </item><item>
            <title>Album five</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album five</link>
            <pubDate>2013 07 19</pubDate>
                </item><item>
            <title>Album four</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album four</link>
            <pubDate>2013 03 23</pubDate>
                </item><item>
            <title>Album one</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album one</link>
            <pubDate>2013 12 11</pubDate>
                </item><item>
            <title>Album three</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album three</link>
            <pubDate>2013 03 23</pubDate>
                </item><item>
            <title>Album two</title>
<description>TEST</description>
<link>http://localhost/index.php?p=Album two</link>
            <pubDate>2013 03 23</pubDate>
                </item><item>
            <title>antiquar</title>
<description>TEST</description>
<link>http://localhost/index.php?p=antiquar</link>
            <pubDate>2013 06 13</pubDate>
                </item><item>
            <title>login</title>
<description>TEST</description>
<link>http://localhost/index.php?p=login</link>
            <pubDate>2013 06 11</pubDate>
                </item><item>
            <title>news</title>
<description>TEST</description>
<link>http://localhost/index.php?p=news</link>
            <pubDate>2013 04 01</pubDate>
                </item><item>
            <title>registreer</title>
<description>TEST</description>
<link>http://localhost/index.php?p=registreer</link>
            <pubDate>2013 04 23</pubDate>
                </item></channel></rss>

2 个答案:

答案 0 :(得分:3)

我会这样做......

<?php
$rss = simplexml_load_file('../../rssfeed.xml');

echo '<h1>'. $rss->channel->title . '</h1>';

foreach ($rss->channel->item as $item) {
    $tmp = str_replace(" ","",$item->pubDate);
    $div[$tmp][] = "<h2><a href='{$item->link}'>{$item->title}</a></h2>" .
                   "<p>{$item->pubDate}</p>" .
                   "<p>{$item->description}</p>";
}
array_multisort($div,SORT_DESC);
foreach($div as $d)
{
    foreach($d as $output) echo $output;
}

?>
顺便说一下......我总是收集一个数组或变量中的数据,以回应它。 如果你有一些问题,你可以轻松地在一个位置上输出输出,而不需要在50行编辑你的代码:) 我添加了替换以从你的pupdate中获取空白。我不知道在arraykey中使用它们时是否会出现空格错误。无论如何......这更干净:) 并且这是一个2级的阵列,因为你有相同的pupdates。

答案 1 :(得分:1)

就这样做:

usort($rss->channel->item, function($a,$b) {
    return strtotime($a->pubdate)-strtotime($b->pubdate);
});

现在,完整代码:

<?php
  $rss = simplexml_load_file('../../rssfeed.xml');
  $json_rss = json_encode($xml);
  $rss = json_decode($json_rss,TRUE);

  usort($rss->channel->item, function($a,$b) {
      return strtotime($a->pubdate)-strtotime($b->pubdate);
  });

  echo '<h1>'. $rss->channel->title . '</h1>';

  foreach ($rss['channel']['item'] as $item) {
     echo '<h2><a href="'. $item['link'] .'">' . $item['title'] . "</a></h2>";
     echo "<p>" . $item['pubDate'] . "</p>";
     echo "<p>" . $item['description'] . "</p>";
  } 
?>
相关问题