Search code examples
phprssrandomparsingfeed

Randomize external RSS feed order in PHP


I'm using a third-party AJAX slideshow for a website that takes an RSS feed as its picture source. I would like to randomize the order of the pictures, but that's not a feature of the slideshow (or the RSS feed I'm pulling from).

Surely it shouldn't be difficult to write a short function in PHP that takes an external RSS feed, randomizes the items and re-publishes the same feed 'out of order'. I just can't seem to make it work.


Solution

  • Are you using DOM XML? Then just shuffle the array on import.

    $xml = new DOMDocument();
    $articles = $xml->getElementsByTagName("article");
    $data = array();
    foreach ($articles as $article) {
        data[] = ...
    }
    shuffle($data);