php 二维数组排序使用详解

昨天要排序数组的时候发现了,要按时间排序,但是php并没有内设这个函数,所以在网上找到了这个代码,第一个参数为数组,第二个是要排序的元素,第三个为排序方式,下面就是php 二维数组排序的代码:

  1. function arraySort($arr, $keys, $type = 'asc') {
  2. $keysvalue = $new_array = array();
  3. foreach ($arr as $k => $v){
  4. $keysvalue[$k] = $v[$keys];
  5. }
  6. $type == 'asc' ? asort($keysvalue) : arsort($keysvalue);
  7. reset($keysvalue);
  8. foreach ($keysvalue as $k => $v) {
  9. $new_array[$k] = $arr[$k];
  10. }
  11. return $new_array;
  12. }
  13. $arr[] = array("name"=>"1","time"=>1) ;

array_multisort(array1,sorting order, sorting type,array2,array3..)是对多个数组或多维数组进行排序的函数。

  1. <?php
  2. function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
  3. if(is_array($arrays)){
  4. foreach ($arrays as $array){
  5. if(is_array($array)){
  6. $key_arrays[] = $array[$sort_key];
  7. }else{
  8. return false;
  9. }
  10. }
  11. }else{
  12. return false;
  13. }
  14. array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
  15. return $arrays;
  16. }
  17. $person = array(
  18. array('id'=>1,'name'=>'fj','weight'=>100,'height'=>180),
  19. array('id'=>2,'name'=>'tom','weight'=>53,'height'=>150),
  20. array('id'=>3,'name'=>'jerry','weight'=>120,'height'=>156),
  21. array('id'=>4,'name'=>'bill','weight'=>110,'height'=>190),
  22. array('id'=>5,'name'=>'linken','weight'=>80,'height'=>200),
  23. array('id'=>6,'name'=>'madana','weight'=>95,'height'=>110),
  24. array('id'=>7,'name'=>'jordan','weight'=>70,'height'=>170)
  25. );
  26. var_dump($person);
  27. $person = my_sort($person,'name',SORT_ASC,SORT_STRING);
  28. var_dump($person);
  29. $person = my_sort($person,'weight');
  30. var_dump($person);
  31. ?>

结果如下:

  1. array (size=7)
  2. 0 =>
  3. array (size=4)
  4. 'id' => int 1
  5. 'name' => string 'fj' (length=2)
  6. 'weight' => int 100
  7. 'height' => int 180
  8. 1 =>
  9. array (size=4)
  10. 'id' => int 2
  11. 'name' => string 'tom' (length=3)
  12. 'weight' => int 53
  13. 'height' => int 150
  14. 2 =>
  15. array (size=4)
  16. 'id' => int 3
  17. 'name' => string 'jerry' (length=5)
  18. 'weight' => int 120
  19. 'height' => int 156
  20. 3 =>
  21. array (size=4)
  22. 'id' => int 4
  23. 'name' => string 'bill' (length=4)
  24. 'weight' => int 110
  25. 'height' => int 190
  26. 4 =>
  27. array (size=4)
  28. 'id' => int 5
  29. 'name' => string 'linken' (length=6)
  30. 'weight' => int 80
  31. 'height' => int 200
  32. 5 =>
  33. array (size=4)
  34. 'id' => int 6
  35. 'name' => string 'madana' (length=6)
  36. 'weight' => int 95
  37. 'height' => int 110
  38. 6 =>
  39. array (size=4)
  40. 'id' => int 7
  41. 'name' => string 'jordan' (length=6)
  42. 'weight' => int 70
  43. 'height' => int 170
  44. array (size=7)
  45. 0 =>
  46. array (size=4)
  47. 'id' => int 4
  48. 'name' => string 'bill' (length=4)
  49. 'weight' => int 110
  50. 'height' => int 190
  51. 1 =>
  52. array (size=4)
  53. 'id' => int 1
  54. 'name' => string 'fj' (length=2)
  55. 'weight' => int 100
  56. 'height' => int 180
  57. 2 =>
  58. array (size=4)
  59. 'id' => int 3
  60. 'name' => string 'jerry' (length=5)
  61. 'weight' => int 120
  62. 'height' => int 156
  63. 3 =>
  64. array (size=4)
  65. 'id' => int 7
  66. 'name' => string 'jordan' (length=6)
  67. 'weight' => int 70
  68. 'height' => int 170
  69. 4 =>
  70. array (size=4)
  71. 'id' => int 5
  72. 'name' => string 'linken' (length=6)
  73. 'weight' => int 80
  74. 'height' => int 200
  75. 5 =>
  76. array (size=4)
  77. 'id' => int 6
  78. 'name' => string 'madana' (length=6)
  79. 'weight' => int 95
  80. 'height' => int 110
  81. 6 =>
  82. array (size=4)
  83. 'id' => int 2
  84. 'name' => string 'tom' (length=3)
  85. 'weight' => int 53
  86. 'height' => int 150
  87. array (size=7)
  88. 0 =>
  89. array (size=4)
  90. 'id' => int 2
  91. 'name' => string 'tom' (length=3)
  92. 'weight' => int 53
  93. 'height' => int 150
  94. 1 =>
  95. array (size=4)
  96. 'id' => int 7
  97. 'name' => string 'jordan' (length=6)
  98. 'weight' => int 70
  99. 'height' => int 170
  100. 2 =>
  101. array (size=4)
  102. 'id' => int 5
  103. 'name' => string 'linken' (length=6)
  104. 'weight' => int 80
  105. 'height' => int 200
  106. 3 =>
  107. array (size=4)
  108. 'id' => int 6
  109. 'name' => string 'madana' (length=6)
  110. 'weight' => int 95
  111. 'height' => int 110
  112. 4 =>
  113. array (size=4)
  114. 'id' => int 1
  115. 'name' => string 'fj' (length=2)
  116. 'weight' => int 100
  117. 'height' => int 180
  118. 5 =>
  119. array (size=4)
  120. 'id' => int 4
  121. 'name' => string 'bill' (length=4)
  122. 'weight' => int 110
  123. 'height' => int 190
  124. 6 =>
  125. array (size=4)
  126. 'id' => int 3
  127. 'name' => string 'jerry' (length=5)
  128. 'weight' => int 120
  129. 'height' => int 156

这里的重点就是,先把要排序的key存到一个一维数组中,然后就可以使用array_multisort()这个函数,将数组按照key进行排序了,当然,这里的排序你完全可以不适用array_multisort()这个函数,仅仅通过foreach遍历也能达到这个效果,但是既然php开发者给我们提供了更好的办法,我们就可以省去不必要的麻烦了。