开发者

Sort Files By Modification Time in PHP

开发者 https://www.devze.com 2023-01-23 05:55 出处:网络
So my current code: $path = \'C:\\\\movies\'; $d = dir($path); $movies = array(); $movietimes = array(); while (false !== ($entry = $d->read()))开发者_如何学编程

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

精彩评论

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

关注公众号