我有一個數組,我需要按照字母順序排序,按名字排序。用數據庫輸入命名數組
我如何命名每個數組元素與firstname
,所以我可以使用排序功能。
功能...
<?php
// fetches all for the users from the table
function fetch_users_directory(){
$result = mysql_query('SELECT `user_lastname` AS `lastname`,
`user_firstname` AS `firstname`,
`user_id` AS `id` FROM `users`');
$users = array();
while(($row = mysql_fetch_assoc($result)) !== false){
$test = mysql_query('SELECT `user_firstname` FROM `users`');
$users[] = $row;
}
return $users;
print_r($user);
?>
結果是...
Array
(
[3] => Array
(
[lastname] => Stoss
[firstname] => Alex
[id] => 1
)
[4] => Array
(
[lastname] => Kennedy
[firstname] => Alice
[id] => 2
)
[5] => Array
(
[lastname] => Williams
[firstname] => Anna
[id] => 3
)
[6] => Array
(
[lastname] => De Jong
[firstname] => Anna
[id] => 4
)
[7] => Array
(
[lastname] => Goodwin
[firstname] => Ash
[id] => 5
)
這可怎麼通過姓名字母順序排列,然後名字?
歡迎StackOverflow上。 ** [請不要將mysql_ *函數用於新代碼。](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)**不再維護並正式棄用。請參閱** [紅盒子](http://php.net/manual/en/function.mysql-connect.php)**?您可以使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli) - [本頁](http://php.net/manual/en /mysqlinfo.api.choosing.php)可以幫助你決定使用哪一個。 – jcsanyi
爲什麼不使用'ORDER_BY firstname,lastname'在查詢中排序呢? –
爲什麼額外的(未使用的)查詢'$ test = mysql_query(...'在你的循環內? – jcsanyi