glob() - 按最后修改的日期时间戳对文件数组进行排序 [英] glob() - sort array of files by last modified datetime stamp
问题描述
我正在尝试按日期(上次修改时间)的顺序显示文件数组.
I'm trying to display an array of files in order of date (last modified).
我已经完成了对数组的循环并将其排序到另一个数组中,但是有没有更简单(更有效)的方法来做到这一点?
I have done this buy looping through the array and sorting it into another array, but is there an easier (more efficient) way to do this?
推荐答案
警告
create_function()
自 PHP 7.2.0 起已弃用.强烈建议不要依赖此功能.
Warning
create_function()
has been DEPRECATED as of PHP 7.2.0. Relying on this function is highly discouraged.
为了后人,如果已接受答案中链接的论坛帖子丢失或有人不清楚,所需的相关代码是:
For the sake of posterity, in case the forum post linked in the accepted answer is lost or unclear to some, the relevant code needed is:
<?php
$myarray = glob("*.*");
usort($myarray, create_function('$a,$b', 'return filemtime($a) - filemtime($b);'));
?>
在我的系统上对此进行了测试并验证它确实按需要按文件 mtime 排序.我也使用了类似的方法(用 Python 编写)来确定我网站上最近更新的文件.
Tested this on my system and verified it does sort by file mtime as desired. I used a similar approach (written in Python) for determining the last updated files on my website as well.
这篇关于glob() - 按最后修改的日期时间戳对文件数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!