Skip to content

Instantly share code, notes, and snippets.

@scarstens
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scarstens/e4683efead24e2e03d41 to your computer and use it in GitHub Desktop.
Save scarstens/e4683efead24e2e03d41 to your computer and use it in GitHub Desktop.
Value Sorting multi-dimensional arrays
/**
* Sorts an array by the value defined in $key
* @author Seth Carstens [seth@sethmatics.com]
*
* example array:
* $array[1]['regularvalue'->'sample1','selectedsortvalue'->'a comes before b']
* $array[2]['regularvalue'->'sample2','selectedsortvalue'->'c comes after b']
* ex array after usort($array, valsort('selectedsortvalue'));:
* $array[2]['regularvalue'->'sample2','selectedsortvalue'->'c comes after b']
* $array[1]['regularvalue'->'sample1','selectedsortvalue'->'a comes before b']
*
* @link http://php.net/manual/en/function.usort.php
*/
function valsort($key) {
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment