Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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