I have an array as follows:
Array(
[27] => \'Sarah Green\',
[29] => \'Adam Brown\',
[68] => \'Fred Able\'
);
I\'d like
You will probably want to create an auxiliar array with the surnames, keep the key, order it and then reconstruct the original array:
function order_by_surname($names) {
foreach ($names as $key => $name) {
$surnames[$key] = array_pop(explode(' ', $name));
}
asort($surnames);
foreach ($surnames as $key => $surname) {
$ordered[$key] = $names[$key];
}
return $ordered;
}
Also, note that you may have problems with this because the surname is not always the last word of a full name when you have two surnames (e.g. John Clark Moore).