note 52398 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:16:16 +0000 |
| Subject: | note 52398 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189721@lists.php.net to get a copy of this message | ||
Note Submitter: tshort at cisco dot com
----
Correction: I meant "passed by value", not "pass by reference". My mistake.
Passing by reference would speed up any of these functions by avoiding the copy necessary for
"by value.
Other solutions have problems with non-arrays as well. But it demonstrates that there are many ways
to solve this problem. Adding checks for non-arrays and short arrays makes the solution less
elegant, but safer. Other solutions should have included similar protections.
function english_list($array, $oxfordComma=0)
{
if (!is_array($array)) return $array;
if (count($array) <= 1) return join(", ", $array);
$last = array_pop($array);
return join(", ", $array) . ($oxfordComma ? "," : "") . " and
" . $last;
}