PHP Snippet 1:
$last_element = array_pop($number_list);
array_push($number_list, 'and '.$last_element);
PHP Snippet 2:
$comma_list = implode(', ', $number_list);
PHP Snippet 3:
$last = array_pop($number_list);
$output = implode(', ', $number_list);
if ($output) {
$output .= ', and ';
}
$output .= $last;
PHP Snippet 4:
$last = array_pop($number_list);
$output = $number_list
? implode(', ', $number_list).', and '.$last
: $last;
PHP Snippet 5:
<?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"
PHP Snippet 6:
$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 . '.';
PHP Snippet 7:
# 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;
PHP Snippet 8:
<?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
?>