0

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";
    }           
?>

6 Answers 6

2

If you want to have different behaviour for the last element, consider changing Amitabh Deotale's answer to something using array_pop()

$last = array_pop($arrayname);
$string = sprintf("%s and %s", implode(", ", $arrayname), $last);
1

Use implode function, for example

implode(",",$arrayname);
1

You can use array_pop() to get the last element of your array.

$array = get_your_array();
$last_el = array_pop($array);

echo join(",", $array) . " and " . $last_el;
0

You can use a foreach to loop over that array using this form

foreach ( $array as $index => $value )

This allows you to test the $index against the count($array) like so

<?php 
    $product = get_field('product');
    $products_sold = get_field('products_sold');
    $arraylength = count($products_sold);
    if ($product == "Pets") {

        $htm = '';
        foreach ($products_sold as $idx => $val ) {
            if ( $idx < count($products_sold) -1) {
                $htm .= "$val, ";
            } else {
                $htm = rtrim($htm, ', ');
                $htm .= " and $val<br />";
            }
        }
        echo $htm;

    } else {
        echo "The product is not a pet";
    }           
?>
6
  • This works beautifully. How would I change it to remove the last comma? Commented Dec 16, 2015 at 13:37
  • I made a small amendment to allow the removal of the odd comma, see if that works for you
    – RiggsFolly
    Commented Dec 16, 2015 at 13:42
  • Still puts the comma in. I tried removing "} else {" thinking that might work but that ruins it! Commented Dec 16, 2015 at 13:57
  • silly me I botched the rtrim() have another look
    – RiggsFolly
    Commented Dec 16, 2015 at 14:00
  • Works perfectly. Thank you so much for this! Commented Dec 16, 2015 at 14:02
0

You can do simply:

  $pets = array("Cats", "Dogs", "Mice", "Lizards");
  foreach ($pets as $key => $value) {
    if ($key == count($pets) - 1) $final .= "and " . $value.".";
    else $final .= $value.", ";
  }
  echo $final;
0

Use the following block of code:

  for($x = 0; $x < $arraylength; $x++){
    $seprator = "";
    if ($x < $arraylength)
    {
        $seprator = ",";
        if ($x == ($arraylength-1))
            $seprator = "and";
    }  // Manage seprator after echo
    echo $products_sold[$x] . $seprator . "<br />";
   }

Not the answer you're looking for? Browse other questions tagged or ask your own question.