Concatenate Array for Human Reading

Avatar of Chris Coyier
Chris Coyier on
function concatenate($elements, $delimiter = ', ', $finalDelimiter = ' and ') {
       $lastElement = array_pop($elements);
       return join($delimiter, $elements) . $finalDelimiter . $lastElement;
}

Usage

$array = array('John', 'Mary', 'Ishmal');
echo concatenate($array);
// outputs "John, Mary and Ishmal"

Provide second parameter, a string, for a delimiter other than a comma.