help with usort cmparison
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Hi Nancy,
I don't know if you ever got this sorted out (no pun intended) but since the data is considerably minimal, copying it into a sortable array and usorting works just fine for me. For example, sorting that URL you mentioned (just outputting dates):
<?php
$xml = simplexml_load_file("http://www.youtube.com/feeds/videos.xml?playlist_id=PLC02CFDE5690E4010");
$entries = array();
//for ($i=0;$i<min(6,count($xml->entry));$i++) {
// use the line above to limit to 6
for ($i=0;$i<count($xml->entry);$i++) {
$entries[] = $xml->entry[$i];
}
usort($entries, function ($a, $b) {
return strcmp($b->published, $a->published);
});
for ($i=0;$i<count($entries);$i++) {
print '<p>' . $i . ': ' . $entries[$i]->published . '</p>';
}
?>
Since the timestamp is in YYYY-MM-DD order a string comparison would be fine. Just flip the strcmp $a and $b to change sort order. This is sorting by newest date first. Feel free to splice in some fake data to the $entries just to show you it's parsing the dates with no trouble or generate your own $entries array in a loop containing objects with $obj->published. You get the general idea. As long as the date stays in that format it'll operate just fine.
Since the time goes in HH:MM:SS you can expect the same accuracy on time as well.
Example output from this on your link:
0: 2015-02-03T15:30:01+00:00
1: 2015-01-27T15:30:07+00:00
2: 2015-01-20T15:30:04+00:00
3: 2015-01-13T15:30:06+00:00
4: 2015-01-06T15:30:04+00:00
5: 2014-12-30T15:30:03+00:00
6: 2014-12-23T15:30:04+00:00
7: 2014-12-16T15:30:04+00:00
8: 2014-12-09T15:27:47+00:00
9: 2014-12-02T15:30:59+00:00
10: 2014-11-25T15:26:00+00:00
11: 2014-11-18T15:26:57+00:00
12: 2014-11-11T15:08:12+00:00
13: 2014-11-04T14:50:55+00:00
14: 2014-10-28T14:30:04+00:00
Copy link to clipboard
Copied
Thanks, J
I'll look at this more closely over the weekend.
Gosh, I need a live-in maid!
Looming holidays are a real productivity killer.
EDIT: The JIVE update seems to have worked.
I haven't been sent to the time-out corner (aka moderator's queue) today.
Nancy O.
Copy link to clipboard
Copied
Exactly my issue. Fall is my crazy season overall. I come here to hide, just wish I had more time, and a maid too!
If I don't talk to you sooner, Happy Turkey Day!

