note 80381 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:19:41 +0000 |
| Subject: | note 80381 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189748@lists.php.net to get a copy of this message | ||
Note Submitter: jimbob
----
My take on a simple english list function - it's similar to what's already here, but calls
count less
function x_implode_eng_list($array, $glue=', ', $final=' and '){
//takes an array and outputs an English style list
//perform the count once
$count = count($array);
//make sure it's an array and has content
if((!is_array($array)) || ($count == 0)) {
return '';
} else {
//pop off the end regardless of length
$end = array_pop($array);
//if there's more than one, implode the remainder using glue and append $end
if($count>1){
return implode($glue, $array) . $final . $end;
} else {
//only one value, return the popped end element
return $end;
}
}
}
$names[] = 'bob';
$names[] = 'john';
$names[] = 'lucy';
echo x_implode_eng_list($names); //will output bob, john and lucy
echo x_implode_eng_list($names, ', ', ' or '); //will output bob, john or lucy