我已经用 usort 尝试过这个,但我没有成功。我有以下数组:
array(
[0] => array(
'date' => '3:6:2012'
),
[1] => array(
'date' => '5:5:2012'
),
[2] => array(
'date' => '20:12:2011'
)
)
日期采用 d:m:yyyy 格式,因此没有前导零。我想对数组进行排序以获得这个:
array(
[0] => array(
'date' => '20:12:2011'
),
[1] => array(
'date' => '5:5:2012'
),
[2] => array(
'date' => '3:6:2012'
)
)
我试过这样:
function cmp($a, $b) {
if($a['date'] = $b['date']) {
return 0;
}
$partsa = explode(":", $a['date']);
$partsb = explode(":", $b['date']);
$daya = $partsa[0];
$montha = $partsa[1];
$yeara = $partsa[2];
$dayb = $partsb[0];
$monthb = $partsb[1];
$yearb = $partsb[2];
if($yeara < $yearb) {
return -1;
} elseif($yeara > $yearb) {
return 1;
} elseif($yeara == $yearb) {
if($montha < $monthb) {
return -1;
} elseif($montha > $monthb) {
return 1;
} elseif($montha == $monthb) {
if($daya < $dayb) {
return -1;
} elseif($daya > $dayb) {
return 1;
} elseif($daya == $dayb) {
return 0;
}
}
}
}
但这并没有给 usort 带来好的结果......如何做到这一点?