Log in

View Full Version : php sort using associative array



Strangeplant
01-24-2007, 06:56 PM
Hi,

I'm using krsort, ksort, arsort and asort in a script. Fine all goes perfectly, except that the sort field is based on file names from the directory contents, and they are (historically) ugly, as in MMDDYY, not YYYYMMDD, causing the different years to be interspersed together. I've read the man page at http://us2.php.net/manual/en/function.sort.php, and quite can't grasp what to do here. My sort code is this:
if($_GET['sort'] == 'alpha'){
if($_GET['mode'] == 'desc'){
krsort($files);
$highlight = 'alpha_desc';
} else {
ksort($files);
$highlight = 'alpha_asc';
}
} else {
if($_GET['mode'] == 'asc'){
asort($files, SORT_NUMERIC);
$highlight = 'date_asc';
} else {
arsort($files, SORT_NUMERIC);
$highlight = 'date_desc';
}
}And my $files array is built like this:
$dir = ".";
$directories = array();
$files = array();
$dir = (substr($dir, -1) == '/') ? substr($dir, 0, -1) : $dir;
if(is_dir($dir)){
if($handle = opendir($dir)){
while(false !== ($file = readdir($handle))){
if($file != "." && $file != ".."){
if(is_dir($dir.'/'.$file)){
$directories[$file] = filemtime($dir.'/'.$file);
} else {
if(strpos($file, '.rar') !== false) $files[$file] = filemtime($dir.'/'.$file);
}
}
}
closedir($handle);
} else {
die('Could not open directory.');
}
} else {
die('Invalid directory.');
}
Could someone explain what I need to do? I figure that I need to make a two dimensional array, say $associated, having three values for each row, one for the alpha sort key, one for the date sort key, and one for the file name. Then I could sort on either $associated[0], or $associated[1], but that doesn't seem to work.....

Twey
01-24-2007, 08:13 PM
Fine all goes perfectly, except that the sort field is based on file names from the directory contents, and they are (historically) ugly, as in MMDDYY, not YYYYMMDD, causing the different years to be interspersed together. I've read the man page at http://us2.php.net/manual/en/function.sort.php, and quite can't grasp what to do here.You probably want uksort (http://www.php.net/uksort)().
function parseMDY($date) {
$month = substr($date, 0, 2);
$day = substr($date, 2, 2);
$year = substr($date, 4);
return array(
'm' => $month,
'd' => $day,
'y' => $year
);
}

function compareMDY($a, $b) {
if($a == $b)
return 0;

$da = parseMDY($a);
$db = parseMDY($b);
$ret = 0;

foreach(array('d', 'm', 'y') as $k)
if($da[$k] < $db[$k])
$ret = -1;
else if($da[$k] > $db[$k])
$ret = 1;
return $ret;
}To sort an array on its keys by an MMDDYY or MMDDYYYY date, pass the latter of the above functions to uksort():
uksort($myArray, "compareMDY");

Strangeplant
01-25-2007, 07:56 PM
Ahhh! Works like a charm. I have never used this function before, so this is great. I just added a reverse sort function and a few more sort variables for the rest of the key.

BTW, it's interesting to note that the array item order in the sort function is reverse from the intuitive
foreach(array('d', 'm', 'y') as $k)sorts with 'y' being the most significant, and 'd' the least.

Thanks Twey, you are great.