我有一个数组
$DATA = array(
array(
"id" => "23",
"rate" => "4.555"
),
array(
"id" => "12",
"rate" => "4.555"
),
array(
"id" => "20",
"rate" => "4.555"
),
array(
"id" => "29",
"rate" => 5.1025"
)
);
现在我需要按键对数组进行排序:rate(升序)和id(升序)。
所以:
function mySort($a, $b) {
return strcmp($a['rate'], $b['rate']);
}
uasort($DATA,'mySort');
现在排序完美但只是按率......
添加新功能:
function mysortID ($a,$b){ //AD
return ($a['id'] > $b['id']) ? 1 : -1;
}
试试吧:
uasort($DATA,'mySort');
uasort($DATA,'mySortID');
但是没有工作......怎么做?
答案 0 :(得分:4)
function mySort($a, $b)
{
// Check the rates
$res = strcmp($a['rate'], $b['rate']);
// If the rates are the same...
if ($res === 0) {
// Then compare by id
$res = $a['id'] > $b['id'] ? 1 : -1;
}
return $res;
}