I am fairly new to php and would love to hear your thoughts on this function I just wrote.
The idea is to sort an array of files chronologically from oldest to newest. Take a look and let me know if you think it can be improved at all:
function sortByDate($filearray){
$OAR = array();
foreach($filearray as $item){
$OAR[filemtime($item)] = $item;
}
ksort($OAR);
return explode(" * ", implode(" * ", $OAR ));
}
Thanks for your time!
If more than one file has the same mtime, it will overwrite that file from your
$OARarray. Instead store each value as an array of files to solve that issue.And this statement
return explode(" * ", implode(" * ", $OAR ));seems like it can just be replace withreturn array_values($OAR);With the above adjustment I mentioned, you may have to loop over the $OAR array and push each set of values to a new array and return that instead.EDIT: here is an example.