Reputation: 484
I am generating a list of cuisines like this:
$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;
The list is then returned as:
1: "American"
5: "Sandwiches"
6: "Barbeque"
8: "Italian"
9: "Mexican"
10: "Sushi"
11: "Burgers"
13: "Japanese"
(The IDs
are according to the database ID). I am trying to sort them now descending by name
but I can't seem to get it done with sort
as it needs a key name. How do I do that?
Upvotes: 0
Views: 151
Reputation: 56
You can use PHP's arsort function. It sorts an array by value in the descending order. Your final code should be like:
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;
I also recommend to check this page to get more info about the PHP array sort functions.
Upvotes: 0
Reputation: 22500
Do Sorting with sql instead of php
$stmt="SELECT cuisine_id, cuisine_name, cuisine_name_trans FROM db_cuisine WHERE 1 ORDER BY cuisine_name DESC ";
Upvotes: 2