我正在生成这样的美食清单:
$lists='';
$stmt="SELECT cuisine_id, cuisine_name, cuisine_name_trans FROM db_cuisine";
if ( $res=$DbExt->rst($stmt)){
if ($list){
foreach ($res as $val) {
$cuisine_json['cuisine_name_trans']=!empty($val['cuisine_name_trans'])?
json_decode($val['cuisine_name_trans'],true):'';
$lists[$val['cuisine_id']]="".qTranslate($val['cuisine_name'],'cuisine_name',$cuisine_json);
}
return $lists;
}
return $res;
}
return false;
然后列表返回为:
1: "American"
5: "Sandwiches"
6: "Barbeque"
8: "Italian"
9: "Mexican"
10: "Sushi"
11: "Burgers"
13: "Japanese"
(IDs
根据数据库ID)。我现在尝试对它们descending by name
进行排序,但是似乎无法通过sort
完成,因为它需要一个键名。我该怎么办?
答案 0 :(得分:2)
使用sql而不是php进行排序
$stmt="SELECT cuisine_id, cuisine_name, cuisine_name_trans FROM db_cuisine WHERE 1 ORDER BY cuisine_name DESC ";
答案 1 :(得分:0)
您可以使用PHP的arsort函数。它按降序对数组进行排序。您的最终代码应类似于:
if ( $res=$DbExt->rst($stmt)){
if ($list){
foreach ($res as $val) {
$cuisine_json['cuisine_name_trans']=!empty($val['cuisine_name_trans'])?
json_decode($val['cuisine_name_trans'],true):'';
$lists[$val['cuisine_id']]="".qTranslate($val['cuisine_name'],'cuisine_name',$cuisine_json);
}
arsort($lists);
return $lists;
}
return $res;
}
return false;
我还建议检查this page以获得有关PHP数组排序功能的更多信息。