There are times when you cannot sort the the order of a data set as you retrieve it from the database, and you need to sort it in script. The function below I wrote a long time ago but use all of the time.
Simply pass in the array, the array key you want to sort on, e.g.
$customerArray = sortArrayByKey ($customerArray, "surname");
function sortArrayByKey ($key, $array, $desc=false){
$sortArray = array();
$returnArray = array();
foreach ($array as $index=>$value) {
$sortArray[$index] = $value[$key];
}
asort ($sortArray);
foreach ($sortArray as $index=>$value) {
$returnArray[] = $array[$index];
}
if ($desc) {
return $returnArray = array_reverse($returnArray);
} else {
return ($returnArray);
}
}