Do you need all these spaces and quotes? You can still use implode , although array_reduce might be nicer
$a = array(1, 2, 3, 4); $x = "'".implode("' , '", $a)."'";
array_reduce :
$x = array_reduce($a, function($b, $c){return ($b===null?'':$b.' , ')."'".$c."'";});
The advantage of array_reduce is that you get NULL for an empty array instead of '' . Please note: you cannot use this built-in function construct in php versions prior to 5.3. You will need to make the callback a separate function and pass your name as a string to array_reduce.
source share