scarstens
6/20/2014 - 11:01 PM

Value Sorting multi-dimensional arrays

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]);
	};
}