Here’s a comma separated string function which uses the serial comma properly with a conjunction in a list. e.g. apple, berry, ice cream => apple, berry and ice cream. It’s essentially a natural language join:

public static function natural_language_join(array $list, $conjunction = 'and') {
$last = array_pop($list);
if ($list) {
return implode(', ', $list) . ' ' . $conjunction . ' ' . $last;
}
return $last;
}

You can customize the conjunction to any word you need.

Support this site, share this page:Share on twitter

Twitter

Share on reddit

Reddit

Share on linkedin

Linkedin

Share on facebook

Facebook

Share on email

Email