$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", $array) . "'";
You could use array_map()
:
function add_quotes($str) {
return sprintf("'%s'", $str);
}
$csv = implode(',', array_map('add_quotes', $array));
DEMO
Also note that there is fputcsv
if you want to write to a file.
$ids = sprintf("'%s'", implode("','", $ids ) );
No, the way that you're doing it is just fine. implode()
only takes 1-2 parameters (if you just supply an array, it joins the pieces by an empty string).
Don't know if it's quicker, but, you could save a line of code with your method:
From
$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
To:
$array = array('lastname', 'email', 'phone');
$comma_separated = "'".implode("','", $array)."'";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With