note 78411 deleted from function.implode by hholzgra
| From: | hholzgra@php.net | Date: | Thu, 02 Aug 2012 13:19:14 +0000 |
| Subject: | note 78411 deleted from function.implode by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-189746@lists.php.net to get a copy of this message | ||
Note Submitter: 1413 at blargh dot com
----
Below is the function for making an English-style list from an array, seems to be simpler than some
of the other examples I've seen.
<?php
function ImplodeProper($arr, $lastConnector = 'and')
{
if( !is_array($arr) or count($arr) == 0) return '';
$last = array_pop($arr);
if(count($arr))
return implode(', ',$arr).", $lastConnector $last";
else
return $last;
}
?>
Examples:
<?
print ImplodeProper(array()).'<br>';
print ImplodeProper(array('foo')).'<br>';
print ImplodeProper(array('foo','bar')).'<br>';
print
ImplodeProper(array('for','bar','bleh')).'<br>';
?>
Yields:
foo
foo, and bar
for, bar, and bleh