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);
Sign up to request clarification or add additional context in comments.

Comments

1

Use implode function, for example

implode(",",$arrayname);

Comments

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;

Comments

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 Comments

This works beautifully. How would I change it to remove the last comma?
I made a small amendment to allow the removal of the odd comma, see if that works for you
Still puts the comma in. I tried removing "} else {" thinking that might work but that ruins it!
silly me I botched the rtrim() have another look
Works perfectly. Thank you so much for this!
|
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;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.