Replace the last comma with an & sign

后端 未结 8 1349
小鲜肉
小鲜肉 2020-12-09 23:21

I have searched everywhere but can\'t find a solution that works for me.

I have the following:

$bedroom_array = array($studio, $one_bed, $two_bed, $t         


        
8条回答
  •  再見小時候
    2020-12-10 00:08

    function fancy_implode($arr){
        array_push($arr, implode(' and ', array_splice($arr, -2)));
        return implode(', ', $arr);
    }
    

    I find this easier to read/understand and use

    • Does not modify the original array
    • Does not use regular expressions as those may fail if strings in the array contain commas, there could be a valid reason for that, something like this: array('Shirts (S, M, L)', 'Pants (72 x 37, 72 x 39)');
    • Delimiters don't have to be of the same length as with some of the other solutions

提交回复
热议问题