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
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;