问题描述
我正在尝试使 scandir();
函数超出其写入限制,我需要的不仅仅是它当前支持的 alpha 排序.我需要对 scandir();
结果进行排序以按修改日期排序.
I'm trying to make scandir();
function go beyond its written limits, I need more than the alpha sorting it currently supports. I need to sort the scandir();
results to be sorted by modification date.
我尝试了在这里找到的一些解决方案以及来自不同网站的其他一些解决方案,但没有一个对我有用,所以我认为我在这里发帖是合理的.
I've tried a few solutions I found here and some other solutions from different websites, but none worked for me, so I think it's reasonable for me to post here.
到目前为止我尝试过的是:
What I've tried so far is this:
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 个图像.
But, this doesn't seem to work for me, it returns 3 results only, but it should return 16 results, because there are 16 images in the folder.
推荐答案
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() 按修改日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!