1. PHP中最普通的数组排序方法 sort();
看个例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 $test = array(); 3 $test[] = 'ABCD'; 4 $test[] = 'aaaa'; 5 $test[] = 'Bdka'; 6 $test[] = '1EEE'; 7 $test[] = '3666'; 8 $test[] = 'cddd'; 9 10 sort($test); 11 foreach($test as $a) 12 echo $a.'<br>'; 13 ?>
运行结果:
由结果可以看出,sort()排序是:先 数字 -> 大写字母 -> 小写字母的顺序排序的
2. 如果非得要不区分大小写来进行排序呢
使用PHP自动的 natcasesort()方法,nat -->native自然;case->大小写;sort->排序
所以从名字上就可以看出这个方法是可以 不区分大小写来进行排序的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 $test = array(); 3 $test[] = 'ABCD'; 4 $test[] = 'aaaa'; 5 $test[] = 'Bdka'; 6 $test[] = '1EEE'; 7 $test[] = '3666'; 8 $test[] = 'cddd'; 9 10 // sort($test); 11 //自然排序,并不区分大小写 12 natcasesort($test); 13 foreach($test as $a) 14 echo $a.'<br>'; 15 16 17 ?>
结果是:
请看,这一次排序的结果是不区分大小写的,不过请注意这个方法对于多维数组一般是得不到想要的结果的
3. PHP的多维数组排序
tips:在PHP中多维数组的排序,一般是以每一个数据项的第一个元素来排序的
看例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 $person = array(); 3 $test['name'] = 'Tommy'; 4 $test['age'] = '1568'; 5 $person[] = $test; 6 7 $test['name'] = 'john'; 8 $test['age'] = '18'; 9 $person[] = $test; 10 11 $test['name'] = '12Boy'; 12 $test['age'] = '20'; 13 $person[] = $test; 14 15 $test['name'] = '3cat'; 16 $test['age'] = '28'; 17 $person[] = $test; 18 19 $test['name'] = 'apple'; 20 $test['age'] = '50'; 21 $person[] = $test; 22 23 $test['name'] = 'Banana'; 24 $test['age'] = '25'; 25 $person[] = $test; 26 27 // sort($test); 28 //自然排序,并不区分大小写 29 natcasesort($person); 30 foreach($person as $a) 31 echo $a['name'].'--'.$a['age'].'<br>'; 32 33 echo '<br><br>===========use sort()==========<br><br>'; 34 sort($person); 35 foreach($person as $a) 36 echo $a['name'].'--'.$a['age'].'<br>'; 37 38 39 ?>
结果是:
使用natcasesort()方法,得到的结果是很奇怪的,而且还有提醒,尽管这不是错误,但看着就是不爽;
而使用sort()方法,明显还是不能区分大小写,难道就真的没有办法可以实现这个需求吗,
不知道PHP内是否有这样的方法,或者高手是怎么解决的,我这里做了一个很笨的操作,看代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 $person = array(); 3 $test['uppname'] = strtoupper('Tommy'); 4 $test['name'] = 'Tommy'; 5 $test['age'] = '1568'; 6 $person[] = $test; 7 8 $test['uppname'] = strtoupper('john'); 9 $test['name'] = 'john'; 10 $test['age'] = '18'; 11 $person[] = $test; 12 13 $test['uppname'] = strtoupper('12Boy'); 14 $test['name'] = '12Boy'; 15 $test['age'] = '20'; 16 $person[] = $test; 17 18 $test['uppname'] = strtoupper('3cat'); 19 $test['name'] = '3cat'; 20 $test['age'] = '28'; 21 $person[] = $test; 22 23 $test['uppname'] = strtoupper('apple'); 24 $test['name'] = 'apple'; 25 $test['age'] = '50'; 26 $person[] = $test; 27 28 $test['uppname'] = strtoupper('Banana'); 29 $test['name'] = 'Banana'; 30 $test['age'] = '25'; 31 $person[] = $test; 32 33 // sort($test); 34 //自然排序,并不区分大小写 35 natcasesort($person); 36 foreach($person as $a) 37 echo $a['name'].'--'.$a['age'].'<br>'; 38 39 echo '<br><br>===========use sort()==========<br><br>'; 40 sort($person); 41 foreach($person as $a) 42 echo $a['name'].'--'.$a['age'].'<br>'; 43 44 45 ?>
思路:增加一个元素,值为需要排序元素的大写/小写 结果
更多信息可以参考:PHP 数组排序