2 回答

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用array_multisort,array_walk排名記錄。例如
$records = [
0 => ['percentage' => 95],
1 => ['percentage' => 91],
2 => ['percentage' => 98],
3 => ['percentage' => 70]
];
array_multisort(array_column($records, 'percentage'),SORT_DESC,$records);
array_walk($records, function(&$v,$k){
$v['rank'] = $k + 1;
});
echo '<pre>';
print_r($records);
輸出
Array
(
[0] => Array
(
[percentage] => 98
[rank] => 1
)
[1] => Array
(
[percentage] => 95
[rank] => 2
)
[2] => Array
(
[percentage] => 91
[rank] => 3
)
[3] => Array
(
[percentage] => 70
[rank] => 4
)
)
- 2 回答
- 0 關(guān)注
- 146 瀏覽
添加回答
舉報(bào)