Blogger Information
Blog 47
fans 3
comment 0
visits 38249
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组排序、数组合并
Original
700 people have browsed it

1.数组排序

  1. <?php
  2. // 数组排序
  3. $arr = ['3'=>'3','a'=>'a','5'=>'5','b'=>'b','1'=>'1'];
  4. // ksort():键名升序
  5. $test = $arr;
  6. ksort($test);
  7. echo 'ksort:',print_r($test,true),'<br>';
  8. // krsort():键名降序
  9. krsort($test);
  10. echo 'krsort:',print_r($test,true),'<br>';
  11. // 按值排序
  12. // arsort():对关联数组按照键值进行降序排序
  13. arsort($arr);
  14. echo 'arsort:',print_r($arr,true),'<br>';
  15. // asort():对关联数组按照键值进行升序排序
  16. asort($arr);
  17. echo 'asort:',print_r($arr,true),'<br>';
  18. // sort:按值升序,重排索引
  19. sort($test);
  20. echo 'sort: ',print_r($test,true),'<br>';
  21. // rsort():按值降序,重排索引
  22. rsort($test);
  23. echo 'rsort: ',print_r($test,true),'<br>';
  24. echo '<hr>';
  25. // uksort():回调函数按键名升序
  26. $test = $arr;
  27. uksort($test,function($a,$b) {
  28. return $a <=> $b;
  29. });
  30. echo 'uksort: ' , print_r($test,true),'<br>';
  31. // uksort():回调函数按键名降序
  32. $test =$arr;
  33. uksort($test,function($a,$b) {
  34. return $b <=> $a;
  35. });
  36. echo 'uksort: ', print_r($test,true),'<br>';
  37. // usort():回调函数按值升序,重排索引
  38. $test = $arr;
  39. usort($test,function($a,$b) {
  40. return $a <=> $b;
  41. });
  42. echo 'usort: ',print_r($test,true),'<br>';
  43. // usort():回调函数按值降序,重排索引
  44. usort($test,function($a,$b) {
  45. return $b <=> $a;
  46. });
  47. echo 'usort: ',print_r($test,true),'<br>';
  48. // 回调函数按值升序,键名不变
  49. $test = $arr;
  50. uasort($test,function($a,$b) {
  51. return $a <=> $b;
  52. });
  53. echo 'uasort: ',print_r($test,true),'<br>';
  54. // 回调函数按值降序,键名不变
  55. uasort($test,function($a,$b) {
  56. return $b <=> $a;
  57. });
  58. echo 'uasort: ',print_r($test,true),'<br>';
  59. echo '<hr>';
  60. // 自然排序:数字在前,字母在后
  61. $arr1 = ['3'=>'3','a'=>'a','5'=>'5','b'=>'b','1'=>'1'];
  62. natsort($arr1);
  63. echo 'natsort: ', print_r($arr1,true),'<br>';
  64. $test = $arr1;
  65. natcasesort($test);
  66. echo 'natcasesort: ', print_r($test,true),'<br>';
  67. ?>

2.数组合并

  1. <?php
  2. // array_merge():把一个或多个数组合并为一个数组,如果两个或更多个数组元素有相同的键名,则最后的元素会覆盖其他元素
  3. $a1 = ['a'=>'red','b'=>'green'];
  4. $a2 = ['c'=>'blue','b'=>'yellow'];
  5. print_r(array_merge($a1,$a2));
  6. echo '<hr>';
  7. // array_intersect():比较数组,返回两个数组的交集
  8. $a1 = ['a'=>'red','b'=>'green','c'=>'blue','d'=>'yellow'];
  9. $a2 = ['e'=>'red','f'=>'green','g'=>'blue'];
  10. $res = array_intersect($a1,$a2);
  11. print_r($res);
  12. echo '<hr>';
  13. // array_unique():删除数组中重复的值
  14. $a = ['a'=>'red','b'=>'green','c'=>'red'];
  15. print_r(array_unique($a));
  16. echo '<hr>';
  17. ?>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments