Surname Sort
<?php
usort($array, function($a, $b) {
// explode name a by spaces
$parts_a = explode(' ', $a);
// remove the last name from the end of the array
$last_a = array_pop($parts_a);
// and shove it on the front
array_unshift($parts_a, $last_a);
// then re-implode into a string
$a = implode(' ', $parts_a);
// repeat the process for name b
$parts_b = explode(' ', $b);
$last_b = array_pop($parts_b);
array_unshift($parts_b, $last_b);
$b = implode(' ', $parts_b);
// perform a case insensitive string comparison
return strcasecmp($a, $b);
});