So I have names stored in a database, but they are stored as firstname lastname in one field. For example field name names
shows john doe
.
I want my name
to be last name first, first name last (ex: doe, john
), so I have that figured out (code below), but after that I need to sort the array by last name, how can I do this?
$names = array(
1 => "Joe Johnson",
2 => "Ann Marie blah",
3 => "person three"
);
foreach ($names as $id => $name) {
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$name = $lastname.", ".$firstname." ";
echo
"<option value='$id'>$name</option>\n";
}