-1

我有一个数组

$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');

但不起作用....怎么办?

4

1 回答 1

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;
}
于 2013-10-21T12:21:23.967 回答