Jump to content

Recommended Posts

Hi guys. I have a list of TV show episodes and air dates in the form of

$episodes[x]["airdate"]=yyyy-mm-dd

but I don't know how to sort the episodes by their air dates. Here is the code I attempted to use to sort by year but it doesn't seem to work. The episode order appears to be basically random.

 

<?php
function recordcompare($record1,$record2){
return strnatcmp($record1,$record2);
};
usort($episodes,"recordcompare");
?>

Link to comment
https://forums.phpfreaks.com/topic/184181-sorting-an-array/
Share on other sites

 

Works great. I don't see why my code with salathe's modification didn't work but thanks for fixing the problem. Final code:

<?php
foreach($episodes as $episode){
$temp289[]=$episode["airdate"];
};
array_multisort($temp289,SORT_ASC,$episodes);
?>

Link to comment
https://forums.phpfreaks.com/topic/184181-sorting-an-array/#findComment-972459
Share on other sites

I'm still not getting the right result. The first four results are:

2008-09-23

2008-09-30

2008-10-14

2008-09-16

 

Perhaps it's time for a more complete snippet so we can see how you're using the values? While you're doing that, this one works as expected:

 

$episodes[1]['airdate'] = '2008-10-14';
$episodes[2]['airdate'] = '2008-09-16';
$episodes[3]['airdate'] = '2008-09-30';
$episodes[4]['airdate'] = '2008-09-23';

function recordcompare($a, $b) {
return strnatcmp($a['airdate'], $b['airdate']);
}
uasort($episodes, 'recordcompare');
print_r($episodes);

Link to comment
https://forums.phpfreaks.com/topic/184181-sorting-an-array/#findComment-972479
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.