I am taking a list of names and putting the last name first. When I use explode to create the $name array, the first name has the first name in array position 0 and the last name at position 1, but the other names all start with first name at position 1 and last name at position 2.
I copied this code from another script that does similar things, but in that code all of the first names, including the first instance, start at number 1 (at least its consistent).
I'm guessing I'm missing something fundamental. I appreciate your assistance.
The code follows.
$_511a = 'Maria Smith, Lance Farquardt, Daniel Berquist, John Barton, Milo Silver';
echo '511a: ' . $_511a . '<br />';
$castsplit = explode(',' , $_511a);
foreach($castsplit as $cast) {
$name = explode(' ',$cast);
$lastname = end($name);
if(count($name) >= 4){
$middlename = $name[2];
} else {
$middlename = null;
}
$firstname = $name[1];
if($middlename){
$castmembers[] = $lastname . ', ' . $firstname . ' ' . $middlename;
} else {
$castmembers[] = $lastname . ', ' . $firstname;
}
}
echo "Corrected names: <br />";
foreach($castmembers as $castmember) {
echo $castmember . '<br />';
}