1
我使用下面的代碼按用戶的名字對用戶列表進行排序,但我需要幫助sortByName函數,因爲我想按姓氏排序列表作爲次要的。所以如果有兩個鮑勃的,而不是隨機排序,它會按姓氏的字母順序排列。我嘗試添加一個if語句時,名字是相同的,但我剛剛結束在某種程度上打破它......php - 由多個變量排序列表
function sortByName($a, $b) {
// if($a['first'] == $b['first']) // commented out because it doesn't work
// return $a['last'] > $b['last'];
// else
return $a['first'] > $b['first']; // this works on its own
}
$a = array(// of usernames //);
$userList = array();
foreach($a as $b) {
$id = $users->fetch_info('id', 'username', $b); // get users' id from their username
$userList[] = $users->userdata($id); // get users' information (like first and last name)
}
usort($userList, 'sortByName');
foreach($userList as $profile) {
$u = $profile['username'];
$first = $profile['first'];
$last = $profile['last'];
include 'user-list.php';
}