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