Skip to main content
Nancy OShea
Community Expert
Community Expert
November 10, 2015
Question

help with usort cmparison

  • November 10, 2015
  • 1 reply
  • 1112 views

Hi Everyone!

It's getting really cold here & I can't seem to wrap my frozen brain cells around this problem. 

Below is my code to parse a YouTube feed which works fine.

I just need to sort entries by their $pubDate and I can't make it work.

<?php

//empty variable for the end

$html = ' ';

//URL of playlist feed

$feed  = "http://www.youtube.com/feeds/videos.xml?playlist_id=PLC02CFDE5690E4010";

//Load feed xml

$xml = simplexml_load_file($feed);

//display 6 entries

for($i = 0; $i < 6; $i++) {

   //node namespace variables

   $published = $xml->entry[$i]->published;

   //optional, shorten date

   $shortDate = date("m/d/Y", strtotime($published));

   $title = $xml->entry[$i]->title;

   $id = $xml->entry[$i]->id;

   //strip unwanted characters from ID

   $id = str_replace ("yt:video:", "", $id);

   $author = $xml->entry[$i]->author->name;

   $uri = $xml->entry[$i]->author->uri;

   //put node variables into html tags & embedded youTube player.

   $html .= "<div class='col-sm-6 col-md-4'>

<h4><a href='$uri'>$title</a></h4>

<iframe src='http://www.youtube.com/embed/$id' allowfullscreen>

</iframe><br>

<small>Published: $shortDate   By: $author</small>

</div><hr>";

   }

//output to html code

echo $html;

?>

Parsed feed results: http://alt-web.com/DEMOS/my_feed.php

Any & all suggestions are appreciated.

Thanks,

Nancy O.

This topic has been closed for replies.

1 reply

David_Powers
Inspiring
November 11, 2015

Not quite sure what you're trying to do. The XML feed already has the videos in descending date order. Do you want them in ascending order? If so, use the SplHeap class like this:

<?php

$feed = simplexml_load_file('https://www.youtube.com/feeds/videos.xml?playlist_id=PLC02CFDE5690E4010');

class SortVideos extends SplHeap

{

    protected function compare($val1, $val2)

    {

        $val1 = (string) $val1->published;

        $val2 = (string) $val2->published;

        if ($val1 == $val2) {

            return 0;

        } elseif ($val1 > $val2) {

            return -1;

        } else {

            return 1;

        }

    }

}

$videos = new SortVideos();

foreach ($feed->entry as $video) {

  $videos->insert($video);

}

foreach ($videos as $video) {

  echo $video->published . '<br>';

}

Inside the second foreach loop, you can access each entry element as $video.

Nancy OShea
Community Expert
Community Expert
November 12, 2015

Hi David,

I appreciate your reply & didn't mean to ignore you.  I just got pulled away by other things...

The actual feed I'm using doesn't sort by date/time stamp, so this approach didn't work for me.  But it gave me some clues about how to proceed once I have time to re-examine this.

Thanks,

Nancy O.

Nancy O'Shea— Product User & Community Expert
David_Powers
Inspiring
November 15, 2015

If you're trying to do a complex sort on an XML feed, the best answer might be to use XSLT and PHP XSL. XSLT isn't the easiest of markup languages to get your head around, but it's very effective, and PHP handles it very well.