I have the following PHP that outputs the contents of my array.
What I want to do is echo out the array with commas in between and put an 'and' before the last in the array.
The code currently outputs say: "Cats Dogs Mice Lizards"
I want it to output: "Cats, Dogs, Mice and Lizards".
Am I able to tell it to add a comma after each array output, apart from the last one and replace it with "and"?
Or do I need to split up the array and treat them all up to the penultimate as one part and then the last in the array as another part?
Thanks for any help.
<?php
$product = get_field('product');
$products_sold = get_field('products_sold');
$arraylength = count($products_sold);
if ($product == "Pets") {
for($x = 0; $x < $arraylength; $x++){
echo $products_sold[$x] . "<br />";
}
} else {
echo "The product is not a pet";
}
?>