Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implode an array with ", " and add "and " before the last item

Tags:

arrays

php

This array holds a list of items, and I want to turn it into a string, but I don't know how to make the last item have a &/and before it instead of a comma.

1 => coke 2=> sprite 3=> fanta 

should become

coke, sprite and fanta 

This is the regular implode function:

$listString = implode(', ', $listArrau); 

What's an easy way to do it?

like image 718
lisovaccaro Avatar asked Dec 21 '11 07:12

lisovaccaro


People also ask

How do you implode an array?

The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.

What are the uses of explode () and implode () functions?

PHP implode() and explode() The implode() function takes an array, joins it with the given string, and returns the joined string. The explode() function takes a string, splits it by specified string, and returns an array.

What is the first parameter of the implode () function?

The first parameter is 'separator' The separator is the optional parameter, and it is there to specify what is needed to be put between the array components. By default, it appears as ”, “ which denotes an empty string. The array values are joined to form a string and are separated by the separator parameter.


2 Answers

A long-liner that works with any number of items:

echo join(' and ', array_filter(array_merge(array(join(', ', array_slice($array, 0, -1))), array_slice($array, -1)), 'strlen')); 

Or, if you really prefer the verboseness:

$last  = array_slice($array, -1); $first = join(', ', array_slice($array, 0, -1)); $both  = array_filter(array_merge(array($first), $last), 'strlen'); echo join(' and ', $both); 

The point is that this slicing, merging, filtering and joining handles all cases, including 0, 1 and 2 items, correctly without extra if..else statements. And it happens to be collapsible into a one-liner.

like image 83
deceze Avatar answered Sep 17 '22 18:09

deceze


I'm not sure that a one liner is the most elegant solution to this problem.

I wrote this a while ago and drop it in as required:

/**  * Join a string with a natural language conjunction at the end.   * https://gist.github.com/angry-dan/e01b8712d6538510dd9c  */ function natural_language_join(array $list, $conjunction = 'and') {   $last = array_pop($list);   if ($list) {     return implode(', ', $list) . ' ' . $conjunction . ' ' . $last;   }   return $last; } 

You don't have to use "and" as your join string, it's efficient and works with anything from 0 to an unlimited number of items:

// null var_dump(natural_language_join(array())); // string 'one' var_dump(natural_language_join(array('one'))); // string 'one and two' var_dump(natural_language_join(array('one', 'two'))); // string 'one, two and three' var_dump(natural_language_join(array('one', 'two', 'three'))); // string 'one, two, three or four' var_dump(natural_language_join(array('one', 'two', 'three', 'four'), 'or')); 
like image 20
Angry Dan Avatar answered Sep 20 '22 18:09

Angry Dan