array to comma string : array_pop « Data Structure « PHP






array to comma string

 
<?
function array_to_comma_string($array) {

    switch (count($array)) {
    case 0:
        return '';

    case 1:
        return reset($array);
    
    case 2:
        return join(' and ', $array);

    default:
        $last = array_pop($array);
        return join(', ', $array) . ", and $last";
    }
}
?>
  
  








Related examples in the same category

1.array_pop( ) returns the value from the end of the array while also removing it from the array.
2.Remove array elements