10

I have an array ($number_list) that has a dynamically generated list of values. There will be at least 1 value in the array and no more than 4.

Currently, I have a nice way of having a comma separated list using this...

$comma_list = implode(', ', $number_list);

However, I'd like to keep with English convention and have the word "and" before the last element. So, let's say $number_list contains the values 4, 5, 6, 7. I would want to echo a statement like, "The list is 4, 5, 6, and 7."

Any ideas how to get that and in there?

hakre
  • 193,403
  • 52
  • 435
  • 836
gtilflm
  • 1,389
  • 1
  • 21
  • 51
  • $comma_list is an array, so the count() function can perfectly fit to tell you how many elements does it have. You need to find the pre-last element ( count($comma_list)-1 ) and append `and` after it – Royal Bg Aug 27 '13 at 22:39

6 Answers6

17

Remove the last element from the list, implode what's left with commas and then concatenate "and last_element":

$last = array_pop($number_list);
$output = implode(', ', $number_list);
if ($output) {
    $output .= ', and ';
}
$output .= $last;

If you prefer, you can also write the above more tersely as

$last = array_pop($number_list);
$output = $number_list
    ? implode(', ', $number_list).', and '.$last
    : $last;

Update:

I have to admit I initially misread the question and thought that it was about replacing the last comma with "and", not adding an "and" before the final item. I have since edited the answer to target the latter scenario. Note that the code can be easily adapted to do either by selecting " and " or ", and " for the "glue" string respectively.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Concatenation for `$output` is reversed. I cannot edit this as not enough characters changed, but just watch out for that. Good answer! `.=` vs `=.` – GhostToast Mar 02 '16 at 16:06
9

Try array_pop() to get the last element, then array_push to modify and push the element back:

http://php.net/manual/en/function.array-pop.php

Something like

$last_element = array_pop($number_list);
array_push($number_list, 'and '.$last_element);

Then you can do your implode:

$comma_list = implode(', ', $number_list);
ChunkyBaconPlz
  • 580
  • 2
  • 10
  • 4
    Nice idea, but this will place an Oxford comma before "and". – Jon Aug 27 '13 at 22:43
  • 2
    @Jon - Which is exactly what he wanted in his original question. – ChunkyBaconPlz Aug 27 '13 at 22:43
  • 4
    Problem with this solution is an array with 0 or 1 element only. So better add code that checks for those cases where it does not work. – hakre Aug 27 '13 at 22:52
  • For those who don't want the Oxford comma, you can do like this: $number_list = Array(1,2,3,4,5); $last_element = array_pop($number_list); echo implode(', ', $number_list) . ' and ' . $last_element; – legibe Dec 29 '21 at 02:57
3

If the list has at least two elements at the end, you can implode them with " and " (or if your question wasn't borked ", and ") already.

Then you implode the whole array with ", ".

Example:

<?php

$numbers = range(1, 4);

array_splice($numbers, -2, 2, implode(' and ', array_slice($numbers, -2)));

echo implode(', ', $numbers); prints "1, 2, 3 and 4"

Demo: https://3v4l.org/DHPla

hakre
  • 193,403
  • 52
  • 435
  • 836
  • The eval.in link is dead. Unless mutating `$numbers` is a concern, this looks reliable to me. https://phpize.online/sql/mysql57/undefined/php/php8/f7f2b5d7bfa0eabed7c275ffbb77b671/ – mickmackusa Apr 22 '22 at 13:49
  • @mickmackusa thx for the hint & link updated to 3v4l.org. cheers. – hakre Apr 22 '22 at 21:03
1
$number_list = array(4, 5, 6, 7);    
$comma_list = strrev(implode(strrev(', and'), explode(strrev(','), strrev(implode(', ', $number_list)), 2)));
echo 'The list is ' . $comma_list . '.';

Output: The list is 4, 5, 6, and 7.

Daerik
  • 4,167
  • 2
  • 20
  • 33
0
# pop the last element off the array and formate text
if( count( $number_list )>1 ){
    $and = " and ".array_pop( $number_list );
}else{
    $and = '';
}
# Implode number list , and  append $and
$comma_list = implode(', ', $number_list).$and;
Binary Alchemist
  • 1,600
  • 1
  • 13
  • 28
0

strrpos

<?php

$number_list= Array(1,2,3,4,5);
$comma_list = implode(', ', $number_list);
$comma_list = substr($comma_list,0,strrpos($comma_list,',')).' And'.substr($comma_list,strrpos($comma_list,',')+1);
echo $comma_list;
//returns 1, 2, 3, 4 And 5
?>
MaveRick
  • 1,181
  • 6
  • 20