$array = array("2011-September_38","2011-June_4","2010-November_9","2011-November_29","2010-December_19");
我想按以下方式对此数组字符串进行排序,应该首先对年份进行排序,然后对月份进行排序,
期望的输出
Array ( [0] => 2010-Marh_19
[1] => 2010-November_9
[2] => 2011-June_4
[3] => 2011-September_38
[4] => 2011-November_29 )
我尝试了一些方法,有人可以将我的功能绑定到年份然后月份http://codepad.org/skEiUkTC
解决方法:
试试这个代码:
<?php
$array = array("2011-September_38","2011-June_4","2010-November_9","2011-November_29","2010-December_19");
function monthCompare($a, $b) {
$da = strtotime(strstr($a, '_', true));
$db = strtotime(strstr($b, '_', true));
return $da > $db;
}
usort($array, "monthCompare");
print_r($array);
?>
输出值
Array
(
[0] => 2010-November_9
[1] => 2010-December_19
[2] => 2011-June_4
[3] => 2011-September_38
[4] => 2011-November_29
)