2

I am trying to make string serial comma from array. Here is the code I use:

<?php
    echo "I eat " . implode(', ',array('satay','orange','rambutan'));
?>

But the results I get:

I eat satay, orange, rambutan

Cannot:

I eat satay, orange, and rambutan

Yet!

So, I made my own function:

<?php   
function array_to_serial_comma($ari,$konj=" and ",$delimiter=",",$space=" "){
    // If not array, then quit 
    if(!is_array($ari)){
        return false; 
    };
    $rturn=array();
    // If more than two 
    // then do actions
    if(count($ari)>2){
        // Reverse array
        $ariBlk=array_reverse($ari,false);
        foreach($ariBlk as $no=>$c){
            if($no>=(count($ariBlk)-1)){ 
                $rturn[]=$c.$delimiter;
            }else{
                $rturn[]=($no==0)? 
                    $konj.$c
                    : $space.$c.$delimiter; 
            };
        };
        // Reverse array
        // to original
        $rturn=array_reverse($rturn,false);
        $rturn=implode($rturn);
    }else{
        // If >=2 then regular merger 
        $rturn=implode($konj,$ari); 
    }; 
    // Return 
    return $rturn; 
 }; 
?>

Thus:

<?php
    $eat = array_to_serial_comma(array('satay','orange','rambutan'));
    echo "I eat $eat";
?>

Result:

I eat satay, orange, and rambutan

Is there a more efficient way, using a native PHP function maybe?

Edit:

Based on code from @Mash, I modifying the code that might be useful:

<?php
function array_to_serial_comma($ari,$konj=" and ",$delimiter=",",$space=" "){
    // If not array, then quit 
    if(!is_array($ari)){
        return false; 
    };
    $rturn=array();
    // If more than two 
    // then do actions
    if(count($ari)>2){
        $akr = array_pop($ari);
        $rturn = implode($delimiter.$space, $ari) . $delimiter.$konj.$akr;
    }else{
        // If >=2 then regular merger 
        $rturn=implode($konj,$ari); 
    }; 
    // Return 
    return $rturn; 
 }; 
?>
bayuah
  • 251
  • 1
  • 5
  • 10

5 Answers5

8

Here's a much cleaner way:

<?php
    $array = array('satay','orange','rambutan');
    $last = array_pop($array);
    echo "I eat " . implode(', ', $array) . ", and " . $last;
?>

array_pop() takes the last element out of the array and assign it to $last

Mash
  • 1,339
  • 7
  • 8
  • I like it, but it's breaking the array... what if we want to use it later on? – Miklos Aubert Aug 13 '13 at 12:37
  • Why not implode(" and ",array(implode(', ', $array),$last)); ? – Thibault Aug 13 '13 at 12:37
  • @AngeloGeels : of course. But I still don't like the idea of tearing apart the array like that. It's a nice hack, but it's still a hack. – Miklos Aubert Aug 13 '13 at 12:39
  • Good idea. But it does not suitable my expectations. Because it result: `I eat satay, orange and rambutan`, not `I eat satay, orange, and rambutan`. See comma (`,`) before `and`. – bayuah Aug 13 '13 at 12:42
  • @bayuah I added the mising comma – Mash Aug 13 '13 at 12:43
  • 2
    Also, with an empty array, it will display "I eat and"... and with an array of only one element, il will yield "I eat and satay" – Miklos Aubert Aug 13 '13 at 12:45
  • But for the two entities so it looks weird. Because being: `I eat satay, and rambutan`. Not: `I'm eating satay and rambutan`. I do not really understand English. But I think if the only two entities do not require a comma before the conjunction. However, I thank you for your help. – bayuah Aug 13 '13 at 12:59
  • Sorry, my last one comment late sent. :P – bayuah Aug 13 '13 at 13:01
  • 1
    @bayuah I think you can put the comma or not, in any number of enumerations (2 and more...) Here's my source http://www.ego4u.com/en/cram-up/writing/comma?08 – Mash Aug 13 '13 at 13:04
  • @Mash A good source. I want to give you 'useful comment' mark on your comment. But unfortunately I not able to do it, yet. :( – bayuah Aug 13 '13 at 13:19
1
<?php
    $arr = array('satay','orange','rambutan');
    print("I eat ".implode(", ", array_slice($arr, 0, count($arr)-1))." and ".$arr[count($arr)-1]);
?>
lePunk
  • 523
  • 3
  • 10
1

Try like this:

$array = array('satay','orange','rambutan');
echo "I eat ".join(' and ', array_filter(array_merge(array(join(', ', array_slice($array, 0, -1))), array_slice($array, -1))));

Duplicate question: Implode array with ", " and add "and " before last item

Community
  • 1
  • 1
Bora
  • 10,529
  • 5
  • 43
  • 73
  • It's verbose, but it could be put away in a function if we need it again. I believe it's cleaner than Mash's answer, and it works better for empty arrays or arrays of 1 element, so you get my vote. It's actually the way I would code this in Python! Also, thanks for finding the duplicate. – Miklos Aubert Aug 13 '13 at 12:55
0
<?php
$arr = array('satay','orange','rambutan');
$lastElement = array_pop($arr);
echo "I eat " . implode(', ',$arr)." and ".$lastElement;
?>

This will result the same : I eat satay, orange and rambutan

K Cloud
  • 271
  • 1
  • 5
  • 15
0

How about this?

function render_array_as_serial_comma($items) {
  $items = $variables['items'];

  if (count($items) > 1) {
    $last = array_pop($items);
    return implode(', ', $items) . ' and ' . $last;
  }
  return array_pop($items);
}

This should do the following:

echo render_array_as_serial_comma(array('a'));
echo render_array_as_serial_comma(array('a', 'b'));
echo render_array_as_serial_comma(array('a', 'b', 'c'));

a
a and b
a, b and c
Nick
  • 2,803
  • 1
  • 39
  • 59