Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2. /**
  3. * Sort a multi-dimensional array by key, then by value.
  4. *
  5. * @param array Array to be sorted
  6. * @param int One of the available sort options: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
  7. * @param int One of the available sort options: SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
  8. * @return void
  9. * @example The following array will be reordered:
  10. * $a = array(
  11. * 'd' => 4,
  12. * 'c' => 2,
  13. * 'a' => 3,
  14. * 'b' => 1,
  15. * 'e' => 2,
  16. * 'g' => 2,
  17. * 'f' => 2,
  18. * );
  19. * SortArrayByKeyThanValue($a); # reorder array to: array(
  20. * 'b' => 1,
  21. * 'c' => 2,
  22. * 'e' => 2,
  23. * 'f' => 2,
  24. * 'g' => 2,
  25. * 'a' => 3,
  26. * 'd' => 4,
  27. * );
  28. * @author Sijmen Ruwhof <sijmen(a)secundity.com>
  29. * @copyright 2011, Secundity
  30. */
  31. function SortArrayByKeyThanValue (&$pArray, $pSortMethodForKey = SORT_ASC, $pSortMethodForValue = SORT_ASC)
  32. {
  33. # check user input: sorting is not necessary
  34. if (count($pArray) < 2)
  35. return;
  36.  
  37. # define $k and $v as array_multisort() needs real variables, as user input is put by reference
  38. $k = array_keys ($pArray);
  39. $v = array_values($pArray);
  40.  
  41. array_multisort(
  42. $v, $pSortMethodForValue,
  43. $k, $pSortMethodForKey
  44. );
  45. $pArray = array_combine($k, $v);
  46. }
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement