开发者

Sort Files By Modification Time in PHP

So my current code:

$path = 'C:\\movies';
$d = dir($path);
$movies = array();
$movietimes = array();
while (false !== ($entry = $d->read()))开发者_如何学编程
{
    if($entry !== '.' AND $entry !== '..')
    {
        $stat = stat($path.'\\'.$entry);
        $movietimes[] = $stat['ctime'];
        $movies[] = $entry;
    }
}
$d->close();
natsort($movietimes);
$movietimes = array_reverse($movietimes, true);
foreach($movietimes as $k=>$v)
{
    echo $movies[$k];
}

However now I want to be able to access three at a time. Any ideas?


Since you already have an array of keys you want to sort the other array according to, you can use array_multisort to do so:

array_multisort($movietimes, SORT_DESC, $movie);

With this the values in $movietimes are sorted in descending order and then used to order the corresponding values in $movie.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜