要利用PHP實現個性化排名,你可以根據用戶的相關屬性(如積分、經驗值、等級等)對用戶進行排序
$users = [
['id' => 1, 'name' => 'Alice', 'points' => 100, 'level' => 2],
['id' => 2, 'name' => 'Bob', 'points' => 200, 'level' => 3],
['id' => 3, 'name' => 'Charlie', 'points' => 150, 'level' => 2],
['id' => 4, 'name' => 'David', 'points' => 180, 'level' => 1],
];
function compareUsers($a, $b) {
if ($a['points'] == $b['points']) {
return $a['level'] - $b['level'];
}
return $b['points'] - $a['points'];
}
usort()
函數和自定義的比較函數對用戶數組進行排序:usort($users, 'compareUsers');
foreach ($users as $user) {
echo "ID: " . $user['id'] . ", Name: " . $user['name'] . ", Points: " . $user['points'] . ", Level: " . $user['level'] . "<br>";
}
上述代碼將輸出以下排序后的用戶數組:
ID: 2, Name: Bob, Points: 200, Level: 3
ID: 3, Name: Charlie, Points: 150, Level: 2
ID: 1, Name: Alice, Points: 100, Level: 2
ID: 4, Name: David, Points: 180, Level: 1
這樣,你就可以根據用戶的積分和等級實現個性化排名了。你可以根據需要調整比較函數以滿足你的需求。