我想根据最终得分对这个数组数组进行排序,并保留我的键。我可以使用usort吗?怎么样 ?

Array
(
[2253472] => Array
    (
        [noOfDays] => 1
        [bestAns] => 1
        [finalScore] => 13.5
    )

[2253465] => Array
    (
        [noOfDays] => 1
        [bestAns] => 0
        [finalScore] => 50
    )

[2253473] => Array
    (
        [noOfDays] => 1
        [bestAns] => 0
        [finalScore] => 23
    )

[2253471] => Array
    (
        [noOfDays] => 1
        [bestAns] => 0
        [finalScore] => 89
    )

[2253464] => Array
    (
        [noOfDays] => 1
        [bestAns] => 1
        [finalScore] => 0.5
    )

[2253415] => Array
    (
        [noOfDays] => 6
        [bestAns] => 0
        [finalScore] => 0.333
    )
)

最佳答案

所有你需要的是

// Ascending
usort($data, function ($a, $b) {
    return $a['finalScore'] - $b['finalScore'];
});


要么

// Descending
usort($data, function ($a, $b) {
    return $b['finalScore'] - $a['finalScore'];
});

关于php - 我可以基于值使用usort在php中对数组进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17081881/

10-11 13:22