2013-12-18 26 views
1

我在php文件中讀取了我的rss.xml文件。 現在我想按降序pubdate排序rss提要。這是可能的有沒有辦法按pubdate按降序排序

<?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> 
+4

uasort是正確的方式! 也許這個鏈接可以幫助你 http://stackoverflow.com/questions/2910611/php-sort-a-multidimensional-array-by-element-containing-date – MaiKaY

回答

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>"; 
    } 
?> 
+0

我得到這個錯誤 警告:usort()期望參數1是數組,在 – Johan

+0

@ user3090697中給出的對象我在代碼中做了一些更改。請現在嘗試。 (我將你的rss xml轉換爲json和數組旁邊,現在usort()的作品 –

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; 
} 

?> 

順便說一下...我總是會收集陣列或可變數據befor echo'ing它。 如果你有一些問題,你可以在一個位置輕鬆打開輸出,並且不需要在50行中編輯你的代碼:) 我添加了替換以獲得你的pupdate中的空白。我不知道空格在arraykey中使用時是否會導致錯誤。無論如何...這是更乾淨:) 它是一個2維數組,因爲你有相同的pupdates。

+1

我同意,我將數據傳遞到一個數組並排序,而不是更改xml – tristanbailey

+0

我得到一個錯誤解析錯誤:語法錯誤,意外'{'我 – Johan

+0

對不起...我不得不做一些'而不是'再試一次 – Dwza