Skip to content

Instantly share code, notes, and snippets.

@ryanburnette
Created April 7, 2017 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburnette/e749d810d2da8e36e471cb90d63ede74 to your computer and use it in GitHub Desktop.
Save ryanburnette/e749d810d2da8e36e471cb90d63ede74 to your computer and use it in GitHub Desktop.
<?php
/*
Adapted from: http://stackoverflow.com/a/8586179/2535178
Usage:
$items = array('Foo', 'Bar', 'Baz');
echo join_sentence($items).'.';
# Foo, Bar and Baz.
*/
function join_sentence($arr) {
$comma = ', ';
$and = ' and ';
$last = array_slice($arr, -1);
$first = join($comma, array_slice($arr, 0, -1));
$both = array_filter(array_merge(array($first), $last), 'strlen');
return join($and, $both);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment