note 73985 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:18:03 +0000 |
| Subject: | note 73985 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189739@lists.php.net to get a copy of this message | ||
Note Submitter: Hayley
----
And adding one more case to drewish at katherinehouse dot com's code to deal with the
two-element case "a and b":
<?php
case 2:
return reset($array).' and '.end($array);
?>
Of course, then one can start considering Oxford rules again, and maybe testing that the argument
really is an array....
<?php
function english_list($array, $useOxfordComma=false)
{
if(!is_array($array))
return '';
switch(count($array))
{
case 0:
return '';
case 1:
// This may not be a normal numerically-indexed array.
return reset($array);
case 2:
return reset($array).' and '.end($array);
default:
$last = array_pop($array);
return implode(', ', $array).($useOxfordComma?',':'').' and
'.$last;
}
}
?>