简体   繁体   English

在php中按字母顺序对数组的结果进行排序

[英]sort the results of array in alphabetical order in php

I am display the brand names with the count of brand number in an array,i want to sort them in alphabetical order.Following is the snippet 我要在阵列中显示品牌名称和品牌编号,我想按字母顺序对它们进行排序。以下是代码段

<?php
foreach($new_array as $BRAND => $n) {

?>
                        <li><input type="checkbox"  class="brand" value="<?php echo $BRAND; ?>" name="Brand"  
                id="roundedOne"  /><label for="roundedOne"><?php echo $BRAND;?> <span class="number_count"><?php echo "($n)"; ?></label></li>
                <?php

}
?>

How can i sort the list to be displayed in alphabetical order.This array is containing the brand names with their count in that.. Please guide me on this.. 我该如何对要按字母顺序显示的列表进行排序。此数组包含品牌名称及其数量。.请指导我。

It looks like you want to sort it on $BRAND which means you want to sort it on the key. 看来您想在$ BRAND上对其进行排序,这意味着您想在键上对其进行排序。 So use ksort . 因此,请使用ksort

Before the foreach just do ksort($new_array); 在foreach之前,只需做ksort($new_array);

Very simple, I think you want to sort it on $BRAND. 很简单,我想您要对$ BRAND进行排序。 So use sort() function. 因此,请使用sort()函数。 Before the foreach loop line just put this code sort($new_array) or see below code. 在foreach循环行之前,只需将此代码排序($ new_array)或参见以下代码。

<?php
   $new_array = array("BB", "DD", "AA", "CC");
   sort($new_array); // New added line code.
   foreach($new_array as $BRAND => $n)
   {
      // Your displaying html + php code.
   }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM