PHP多维数组排序array详解

这篇文章主要为大家详细介绍了PHP多维数组排序array的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

PHP数组Array按字段排序

  1. /**
  2. * Sort array by filed and type, common utility method.
  3. * @param array $data
  4. * @param string $sort_filed
  5. * @param string $sort_type SORT_ASC or SORT_DESC
  6. */
  7. public function sortByOneField($data, $filed, $type)
  8. {
  9. if (count($data) <= 0) {
  10. return $data;
  11. }
  12. foreach ($data as $key => $value) {
  13. $temp[$key] = $value[$filed];
  14. }
  15. array_multisort($temp, $type, $data);
  16. return $data;
  17. }

PHP数组Array按二维排序,先按第一个字段排序,再按第二个字段排序

  1. /**
  2. * Sort array by filed and type, common utility method.
  3. * @param array $array
  4. * @param string $filed1
  5. * @param string $type1 SORT_ASC or SORT_DESC
  6. * @param string $filed2
  7. * @param string $type2 SORT_ASC or SORT_DESC
  8. */
  9. public function sortByTwoFiled($data, $filed1, $type1, $filed2, $type2)
  10. {
  11. if (count($data) <= 0) {
  12. return $data;
  13. }
  14. foreach ($data as $key => $value) {
  15. $temp_array1[$key] = $value[$filed1];
  16. $temp_array2[$key] = $value[$filed2];
  17. }
  18. array_multisort($temp_array1, $type1, $temp_array2, $type2, $data);
  19. return $users;
  20. }

sortMultiArray()最多支持3维数组排序,当然可以扩充的,自定义方法重载实现对多维数字的排序,这里的多维是说数据的多个字段。

使用方法:

1. sortMultiArray($data, [‘score' => SORT_DESC])

2. sortMultiArray($data, [‘score' => SORT_DESC, ‘count' => SORT_ASC])

3. sortMultiArray($data, [‘score' => SORT_DESC, ‘count' => SORT_ASC, ‘name' => SORT_ASC])

  1. /**
  2. * Sort multi array by filed and type.
  3. * @param data $array
  4. * @param condition $array
  5. */
  6. public function sortMultiArray(&$data, $condition)
  7. {
  8. if (count($data) <= 0 || emptyempty($condition)) {
  9. return $data;
  10. }
  11. $dimension = count($condition);
  12. $fileds = array_keys($condition);
  13. $types = array_values($condition);
  14. switch ($dimension) {
  15. case 1:
  16. $data = $this->sort1Dimension($data, $fileds[0], $types[0]);
  17. break;
  18. case 2:
  19. $data = $this->sort2Dimension($data, $fileds[0], $types[0], $fileds[1], $types[1]);
  20. break;
  21. default:
  22. $data = $this->sort3Dimension($data, $fileds[0], $types[0], $fileds[1], $types[1], $fileds[2], $types[2]);
  23. break;
  24. }
  25. return $data;
  26. }
  27. public function sort1Dimension(&$data, $filed, $type)
  28. {
  29. if (count($data) <= 0) {
  30. return $data;
  31. }
  32. foreach ($data as $key => $value) {
  33. $temp[$key] = $value[$filed];
  34. }
  35. array_multisort($temp, $type, $data);
  36. return $data;
  37. }
  38. public function sort2Dimension(&$data, $filed1, $type1, $filed2, $type2)
  39. {
  40. if (count($data) <= 0) {
  41. return $data;
  42. }
  43. foreach ($data as $key => $value) {
  44. $sort_filed1[$key] = $value[$filed1];
  45. $sort_filed2[$key] = $value[$filed2];
  46. }
  47. array_multisort($sort_filed1, $type1, $sort_filed2, $type2, $data);
  48. return $data;
  49. }
  50. public function sort3Dimension(&$data, $filed1, $type1, $filed2, $type2, $filed3, $type3)
  51. {
  52. if (count($data) <= 0) {
  53. return $data;
  54. }
  55. foreach ($data as $key => $value) {
  56. $sort_filed1[$key] = $value[$filed1];
  57. $sort_filed2[$key] = $value[$filed2];
  58. $sort_filed3[$key] = $value[$filed3];
  59. }
  60. array_multisort($sort_filed1, $type1, $sort_filed2, $type2, $sort_filed3, $type3, $data);
  61. return $data;
  62. }