Valleriani
asked on
PHP -- Sort array via modified time?
I have this:
function getFiles($dir,$exclude='') {
$handle = opendir($dir);
for(;(false !== ($readdir = readdir($handle)));){
if($readdir != '.' && $readdir != '..' && $readdir!=$exclude){
$path = $dir.'/'.$readdir;
if(is_file($path) && !strstr($path,"___")) {
$output[] = array('name'=>$readdir,'mt ime'=>file mtime($pat h));
}
}
}
closedir($handle);
return isset($output)?$output:fal se;
}
mtime is defined as modified time of the file using filemtime.. So if I do something like
$files = getFiles($pathTemp,'index. html');
It will give me an array of files, and I can call it via
foreach ($files as $file) {
echo $file['name'];
echo $file['mtime'];
}
But now that I have this array, how can I sort it properly? I want it to be OLDEST to NEWEST files, so that the oldest files are processed first.. Thanks!
function getFiles($dir,$exclude='')
$handle = opendir($dir);
for(;(false !== ($readdir = readdir($handle)));){
if($readdir != '.' && $readdir != '..' && $readdir!=$exclude){
$path = $dir.'/'.$readdir;
if(is_file($path) && !strstr($path,"___")) {
$output[] = array('name'=>$readdir,'mt
}
}
}
closedir($handle);
return isset($output)?$output:fal
}
mtime is defined as modified time of the file using filemtime.. So if I do something like
$files = getFiles($pathTemp,'index.
It will give me an array of files, and I can call it via
foreach ($files as $file) {
echo $file['name'];
echo $file['mtime'];
}
But now that I have this array, how can I sort it properly? I want it to be OLDEST to NEWEST files, so that the oldest files are processed first.. Thanks!
Hmm.. You might want this, too. Not sure about that.
http://us2.php.net/manual/en/function.uasort.php
Either function might meet your needs. Example #2 on usort() looks particularly on point.
http://us2.php.net/manual/en/function.uasort.php
Either function might meet your needs. Example #2 on usort() looks particularly on point.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
JH: That is a nifty piece of code! ~Ray
ASKER
Thank you both! Both pieces of coding did the trick. Thanks!
http://us2.php.net/manual/en/function.usort.php