-1

I have the following piece of code that returns a number of categories, along with a counter of items in each category. The problem is that it's not returned alphabetically. How can I force them to be output alphabetically? I think it has to be with "asort" or something but I can't seem to figure out how. I'd greatly appreciate any advice! :)

The array to be alphabetically sorted is:

 $category_lang[$cat_details['category_id']]

The complete code:

 while ($cat_details = $db->fetch_array($sql_select_categories))
 {
  $subcat_link = basename($_SERVER['PHP_SELF']) . '?parent_id=' .$cat_details['category_id'] . $additional_vars;

  $categoryCounter = (COUNT_CATS == 1 && !empty($src_details['keywords_search'])) ? $cat_counter[$cat_details['category_id']] : $cat_details['items_counter'];

  if ($categoryCounter > 0 || COUNT_CATS == 0) {
  $output .= '<tr> ' .
        '   <td class="contentfont"><a href="' . $subcat_link . '">' . $category_lang[$cat_details['category_id']] . '</a> ' .
        (($categoryCounter) ? '<span class="cnt">(' . $categoryCounter . ')</span>' : '') .
        '  </td> ' .
        '</tr> ';
  }
}
4

2 回答 2

0
$array=$category_lang[$cat_details['category_id']]

asort($array);
 //Now $array is sorted alphabetically. use it as you want.

您可以通过数组元素使用 foreach 循环。

asort,对维护关联元素索引的数组进行排序。

于 2012-08-08T10:50:56.643 回答
0

在这种情况下,最好使用 mysql ORDER BY name ASC

于 2012-08-08T11:06:52.960 回答