SCANDIR()按日期排序修改 [英] scandir() to sort by date modified

查看:203
本文介绍了SCANDIR()按日期排序修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 SCANDIR(); 功能超越其书面极限,我需要比阿尔法排序它目前支持。我需要排序的 SCANDIR(); 结果被修改日期进行排序。

我已经尝试了一些解决方案,我发现这里和来自不同网站的一些其他的解决方案,但没有为我工作,所以我认为这是合理的,我在这里发表。

什么到目前为止,我已经试过是这样的:

 函数scan_dir($ DIR)
{
    $ files_array = SCANDIR($目录);
    $ img_array =阵列();
    $ img_dsort =阵列();
    $ final_array =阵列();    的foreach($ files_array为$文件)
    {
        !,如果(($文件=)及和放大器;!($文件=..)及及($文件=.svn文件)及!&安培;!($文件=。 htaccess的))
        {
            $ img_array [] = $文件;
            $ img_dsort [] = filemtime(。$目录'/'$文件);
        }
    }    $ merge_arrays = array_combine($ img_dsort,$ img_array);
    krsort($ merge_arrays);    的foreach($ merge_arrays为$关键=> $值)
    {
        $ final_array [] = $价值;
    }    回报(is_array($ final_array))? $ final_array:假的;
}

不过,这似乎并没有为我工作,它只有3个结果返回,但它应该返回16的结果,因为有文件夹中的16个图像。

不知道如何解决这个问题?

感谢。


解决方案

 函数scan_dir($ DIR){
    $忽略=阵列('..','.svn文件','的.htaccess''。');    $文件=阵列();
    的foreach(SCANDIR($ DIR)为$文件){
        如果(in_array($文件,$忽略))继续;
        $文件[$文件] = filemtime($目录'/'$文件);
    }    arsort($文件);
    $文件= array_keys($文件);    返回($文件)? $文件:假的;
}

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;
}

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.

Any idea how to fix this problem?

Thanks.

解决方案

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()按日期排序修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆