how to separate array values by "," and "and"

1

Hello, I have an array and I need to print it on the separate screen by, and ex ..

<select name="quitacao[um]"class="large m-wrap" data-trigger="hover"  required="" > 
<select name="quitacao[dois]"class="large m-wrap" data-trigger="hover" required="" >

And so it goes

Then I'll have an array on the next screen

If I do this

implode(",",$quitacao);

I am able to separate everything by commas but I need the last one to get E

ex .. January, February, March, and November

    
asked by anonymous 04.09.2015 / 01:51

2 answers

3

Take all the elements of the array to the penultimate one and concatenate them with a comma; then concatenate the latter with an "e":

function concatena($meses)
{
    $string  = implode(', ', array_slice($meses, 0, -1));
    $string .= ' e ' . $meses[count($meses)-1];

    return $string;
}
    
04.09.2015 / 02:00
3
$arr = ['janeiro', 'fevereiro', 'março', 'abril', 'maio'];
$l = array_pop($arr);
echo implode( ', ', $arr ) . ' e ' . $l;

The function array_pop() removes the last element from the array and returns its value.

The variable $l gets the value, so you can use it in the concatenation.

In case you need to use the array in its original form, simply return the value removed by the array_pop() function.

$arr[] = $l;
    
04.09.2015 / 02:00