note 51907 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:15:43 +0000 |
| Subject: | note 51907 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189716@lists.php.net to get a copy of this message | ||
Note Submitter: davidpk212 at gmail dot com
----
I made this function to create an english-readable list from an array.
<?php
function english_list($array, $oxfordcomma=1) {
$count = count($array)-1;
$last = $array[$count];
unset($array[$count]);
$str = join(", ", $array);
if ($oxfordcomma) {
$str .= ",";
}
$str .= " and $last";
return $str;
}
?>
The optional parameter "oxfordcomma" indicates whether or not to use the Oxford comma (a
comma before the "and").
Example:
<?php
print english_list(array("foo", "bar", "foobar", "barfoo"));
?>
Would produce:
foo, bar, foobar, and barfoo