0

I have a PHP script that is recursively getting files from a directory tree. Currently it is sorting by "newest added" date.

The filenames are variable, but all end in 8 digit numbers that represent yyyymmdd

So we could have

MyFileName-20180102 DifferentName3-20171231 MyName-20170704

Here's the code I have:

// Loop recursively through all directories in specified path and list all files
$files = array();
function getFiles($strDocRoot, $path, &$files) {
  foreach (glob("$path/*") as $node) {
    if (!is_dir($node)) {
      $files[str_replace($strDocRoot, '', $node)] = filemtime($node);
    } else {
      getFiles($strDocRoot, $node, $files);
    }
  }
}
getFiles($strDocRoot, "$strDocRoot$feed", $files);
arsort($files);

What I need is to get this array sorted by those ending dates in descending order. I'm not quite sure how the arsort is determining its method of sorting.

Any help would be appreciated.

  • arsort() uses keys in reverse order; see http://php.net/manual/en/array.sorting.php – Kevin_Kinsey Jan 02 '18 at 22:22
  • 1
    [usort](http://php.net/manual/en/function.usort.php) can be helpful since you can define sorting based on the last 8 characters. – mcon Jan 02 '18 at 22:23

0 Answers0