PHP自定义数组按年份和月份排序文件名,最新的第一个

I've got an array of files which looks like this:

array (
  0 => 'scpt-01-2010.phtml',
  1 => 'scpt-01-2011.phtml',
  2 => 'scpt-02-2010.phtml',
  3 => 'scpt-02-2011.phtml',
  4 => 'scpt-03-2010.phtml',
  5 => 'scpt-04-2010.phtml',
  6 => 'scpt-05-2010.phtml',
  7 => 'scpt-06-2010.phtml',
  8 => 'scpt-07-2010.phtml',
  9 => 'scpt-08-2010.phtml',
  10 => 'scpt-09-2010.phtml',
  11 => 'scpt-10-2010.phtml',
  12 => 'scpt-11-2010.phtml',
  13 => 'scpt-12-2010.phtml',
);

How can I sort it so that 2011 files appear first, in order of month (so it should lead with scpt-02-2011.phtml)?

I've tried the main sorting functions like natsort, rsort, arsort etc but I'm not getting anywhere fast!

Thanks in advance.

function customSort($a, $b) {
    // extract the values with a simple regular expression
    preg_match('/scpt-(\d{2})-(\d{4})\.phtml/i', $a, $matches1);
    preg_match('/scpt-(\d{2})-(\d{4})\.phtml/i', $b, $matches2);

    // if the years are equal, compare by month
    if ($matches2[2] == $matches1[2]) {
        return $matches2[1] - $matches1[1];
    // otherwise, compare by year
    } else {
        return $matches2[2] - $matches1[2];
    }
}

// sort the array
usort($array, 'customSort');

This method uses usort() to sort the array, passing the name of our comparison function.

http://php.net/usort

Use usort(), it allows you to write your own callback and sort your own way.