glob()-按日期排序 [英] glob() - sort by date
问题描述
我正在尝试按日期顺序显示文件数组(最后修改).
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?
推荐答案
从PHP 7.2.0开始,警告
create_function()
已被废弃.强烈建议不要使用此功能.
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屋!