note 86845 modified in function.implode by thiago
| From: | thiago@php.net | Date: | Thu, 06 Nov 2008 11:49:11 +0000 |
| Subject: | note 86845 modified in function.implode by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146208@lists.php.net to get a copy of this message | ||
This function is much more elegant and fast. It converts array to the list with 'and' at
the end:
<?php
function ImplodeToEnglish ($array) {
// sanity check
if (!$array || !count ($array))
return '';
// get last element
$last = array_pop ($array);
// if it was the only element - return it
if (!count ($array))
return $last;
return implode (', ', $array).' and '.$last;
}
?>
--was--
This function is much more elegant and fast. It converts array to the list with 'and' at
the end:
function ImplodeToEnglish ($array) {
// sanity check
if (!$array || !count ($array))
return '';
// get last element
$last = array_pop ($array);
// if it was the only element - return it
if (!count ($array))
return $last;
return implode (', ', $array).' and '.$last;
}
http://php.net/manual/en/function.implode.php