Jump to content

Sorting an array


cgimusic

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.