Sort () &rsort () &ksort () &asort () &arsort () &krsort ()

Source: Internet
Author: User

Ascending: Descending from low to High: from high to low sort ()sorting an array in ascending order by value will delete the original key name and re-assign the new name
Rsort ()the array is descending by valueSort, will be Delete the original key nameand re-assigned the new name
Ksort ()The array is sorted in ascending order by key name, preserving the corresponding relationship between the original name and the value
Krsort ()The array is sorted by key name in descending order, preserving the corresponding relationship between the original name and the value

Asort ()The array is sorted in ascending order by value, preserving the corresponding relationship between the original name and the value
Arsort ()The array is sorted in descending order by value, preserving the corresponding relationship between the original name and the value
Example:
  
 
  1. <?php
  2. //create the movies
  3. $movies=array(
  4. ‘gangtiexia‘ =>10,
  5. ‘jianbingxia‘=>10,
  6. ‘zhuzhuxia‘=>6,
  7. ‘doupo‘=>9,
  8. ‘dota‘=>7
  9. );
  10. //初始数组
  11. echo ‘<tr><td><b>original order</b></td></tr><br>‘;
  12. foreach ($movies as $title => $grade) {
  13. # code...
  14. echo "<tr><td>$grade</td>
  15. <td>$title</td></tr><br>";
  16. }
  17. echo ";
  18. //ksort()排序
  19. echo ‘<tr><td ><b>ksort by title</b></td></tr><br>‘;
  20. ksort($movies);
  21. foreach ($movies as $title => $grade) {
  22. # code...
  23. echo "<tr><td>$grade</td>
  24. <td>$title</td></tr><br>";
  25. }
  26. //krsort()排序
  27. echo ‘<tr><td ><b>krsort by title</b></td></tr><br>‘;
  28. krsort($movies);
  29. foreach ($movies as $title => $grade) {
  30. # code...
  31. echo "<tr><td>$grade</td>
  32. <td>$title</td></tr><br>";
  33. }
  34. echo ";
  35. //asort()排序
  36. echo ‘<tr><td><b>asort by grade</b></td></tr><br>‘;
  37. asort($movies);
  38. foreach ($movies as $title => $grade) {
  39. # code...
  40. echo "<tr><td>$grade</td>
  41. <td>$title</td></tr><br>";
  42. }
  43. //arsort()排序
  44. echo ‘<tr><td><b>arsort by grade</b></td></tr><br>‘;
  45. arsort($movies);
  46. foreach ($movies as $title => $grade) {
  47. # code...
  48. echo "<tr><td>$grade</td>
  49. <td>$title</td></tr><br>";
  50. }
  51. echo ";
  52. //sort()排序
  53. echo ‘<tr><td><b>sort by </b></td></tr><br>‘;
  54. sort($movies);
  55. foreach ($movies as $title => $grade) {
  56. # code...
  57. echo "<tr><td>$grade</td>
  58. <td>$title</td></tr><br>";
  59. }
  60. //rsort()排序
  61. echo ‘<tr><td><b>rsort by </b></td></tr><br>‘;
  62. rsort($movies);
  63. foreach ($movies as $title => $grade) {
  64. # code...
  65. echo "<tr><td>$grade</td>
  66. <td>$title</td></tr><br>";
  67. }
  68. ?>
Results:


From for notes (Wiz)

Sort () &rsort () &ksort () &asort () &arsort () &krsort ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.