Skip to content

Instantly share code, notes, and snippets.

@austinjreilly
Created February 28, 2014 02:03
Show Gist options
  • Save austinjreilly/9263785 to your computer and use it in GitHub Desktop.
Save austinjreilly/9263785 to your computer and use it in GitHub Desktop.
Takes array of people and outputs a single name, 2 names separated by an ampersand, or 3 or more names separated by a comma and the last one by an ampersand
function person_list( $personArray ) {
$personCount = count( $personArray );
for ( $i = 0; $i < $personCount; $i++ ) {
$name = $personArray[$i]['name'];
$personHTML .= "<span class=\"name\">{$name}";
if ( $personCount == 2 ){
if ( $i < ($personCount - 1 ) ) {
$personHTML .= " &amp; ";
}
} elseif ( $personCount > 2 ) {
if ( $i < ($personCount - 2 ) ) {
$personHTML .= ", ";
} elseif ( $i < ( $personCount - 1 ) ) {
$personHTML .= ", &amp; ";
}
}
$personHTML .= "</span>";
}
return $personHTML;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment