I want to add a comma to every item except the last one. The last one must have \"and\".
Item 1, Item 2 and Item 3
But items can be from 1 +
So if on
Here's a variant that has an option to support the controversial Oxford Comma and takes a parameter for the conjunction (and/or). Note the extra check for two items; not even Oxford supporters use a comma in this case.
function conjoinList($items, $conjunction='and', $oxford=false) {
$count = count($items);
if ($count === 0){
return '';
} elseif ($count === 1){
return $items[0];
} elseif ($oxford && ($count === 2)){
$oxford = false;
}
return implode(', ', array_slice($items, 0, -1)) . ($oxford? ', ': ' ') . $conjunction . ' ' . end($items);
}
You can do it like this:
$items = array("Item 1", "Item 2", "Item 3", "Item 4");
$item = glueItems($items);
function glueItems($items) {
if (count($items) == 1) {
$item = implode(", ", $items);
} elseif (count($items) > 1) {
$last_item = array_pop($items);
$item = implode(", ", $items) . ' and ' . $last_item;
} else {
$item = '';
}
return $item;
}
echo $item;
Well if it is an array just use implode(...)
Example:
$items = array("Item 1", "Item 2", "Item 3", "Item 4");
$items[count($items) - 1] = "and " . $items[count($items) - 1];
$items_string = implode(", ", $items);
echo $items_string;
Demo: http://codepad.viper-7.com/k7xISG
minitech's solution on the outset is elegant, except for one small issue, his output will result in:
var_dump(makeList(array('a', 'b', 'c'))); //Outputs a, b and c
But the proper formatting of this list (up for debate) should be; a, b, and c. With his implementation the next to last attribute will never have ',' appended to it, because the array slice is treating it as the last element of the array, when it is passed to implode()
.
Here is an implementation I had, and properly (again, up for debate) formats the list:
class Array_Package
{
public static function toList(array $array, $conjunction = null)
{
if (is_null($conjunction)) {
return implode(', ', $array);
}
$arrayCount = count($array);
switch ($arrayCount) {
case 1:
return $array[0];
break;
case 2:
return $array[0] . ' ' . $conjunction . ' ' . $array[1];
}
// 0-index array, so minus one from count to access the
// last element of the array directly, and prepend with
// conjunction
$array[($arrayCount - 1)] = $conjunction . ' ' . end($array);
// Now we can let implode naturally wrap elements with ','
// Space is important after the comma, so the list isn't scrunched up
return implode(', ', $array);
}
}
// You can make the following calls
// Minitech's function
var_dump(makeList(array('a', 'b', 'c')));
// string(10) "a, b and c"
var_dump(Array_Package::toList(array('a', 'b', 'c')));
// string(7) "a, b, c"
var_dump(Array_Package::toList(array('a', 'b', 'c'), 'and'));
string(11) "a, b, and c"
var_dump(Array_Package::toList(array('a', 'b', 'c'), 'or'));
string(10) "a, b, or c"
Nothing against the other solution, just wanted to raise this point.
You can implode the X - 1 items with a comma and add the last one with "and".
Here’s a function for that; just pass the array.
function make_list($items) {
$count = count($items);
if ($count === 0) {
return '';
}
if ($count === 1) {
return $items[0];
}
return implode(', ', array_slice($items, 0, -1)) . ' and ' . end($items);
}
Demo