Value Sorting multi-dimensional arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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