scandir()按修改日期排序
我试图使scandir();功能超出其书面限制,我需要的不仅仅是当前支持的alpha排序。我需要对scandir();结果进行排序,以按修改日期排序。
我尝试了一些在这里找到的解决方案,以及其他来自不同网站的解决方案,但是没有一个适合我,因此我认为在这里发布是合理的。
到目前为止,我尝试过的是:
function scan_dir($dir){
    $files_array = scandir($dir);
    $img_array   = array();
    $img_dsort   = array();
    $final_array = array();
    foreach($files_array as $file)
    {
        if(($file != ".") && ($file != "..") && ($file != ".svn") && ($file != ".htaccess"))
        {
            $img_array[] = $file;
            $img_dsort[] = filemtime($dir . '/' . $file);   
        }
    }
    $merge_arrays = array_combine($img_dsort, $img_array);
    krsort($merge_arrays);
    foreach($merge_arrays as $key => $value)
    {
        $final_array[] = $value;    
    }
    return (is_array($final_array)) ? $final_array : false;
}
但是,这似乎对我不起作用,它仅返回3个结果,但应该返回16个结果,因为文件夹中有16张图像。
回答:
function scan_dir($dir) {    $ignored = array('.', '..', '.svn', '.htaccess');
    $files = array();    
    foreach (scandir($dir) as $file) {
        if (in_array($file, $ignored)) continue;
        $files[$file] = filemtime($dir . '/' . $file);
    }
    arsort($files);
    $files = array_keys($files);
    return ($files) ? $files : false;
}
以上是 scandir()按修改日期排序 的全部内容, 来源链接: utcz.com/qa/433796.html








