php implode (101) with quotes
PhpArraysStringCsvImplodePhp Problem Overview
Imploding a simple array
would look like this
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
and that would return this
lastname,email,phone
great, so i might do this instead
$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
and now i have what I want a nice pretty csv string
'lastname','email','phone'
is there a better way to do this, it feels to me like there should be an optional parameter for implode am I missing something ?
Php Solutions
Solution 1 - Php
$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", $array) . "'";
Solution 2 - Php
You could use array_map()
:
function add_quotes($str) {
return sprintf("'%s'", $str);
}
$csv = implode(',', array_map('add_quotes', $array));
Also note that there is fputcsv
if you want to write to a file.
Solution 3 - Php
$ids = sprintf("'%s'", implode("','", $ids ) );
Solution 4 - Php
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).
Solution 5 - Php
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)."'";
Solution 6 - Php
If you want to use loops you can also do:
$array = array('lastname', 'email', 'phone');
foreach($array as &$value){
$value = "'$value'";
}
$comma_separated = implode(",", $array);
Solution 7 - Php
$id = array(2222,3333,4444,5555,6666);
$ids = "'".implode("','",$id)."'";
Or
$ids = sprintf("'%s'", implode("','", $id ) );
Solution 8 - Php
Alternatively you can create such a function:
function implode_with_quotes(array $data)
{
return sprintf("'%s'", implode("', '", $data));
}
Solution 9 - Php
try this code :
$data = 'ABC,DEF,GHI';
$str = "'".str_replace(',',"','",$data)."'";
echo $str;
Solution 10 - Php
If you want to avoid the fopen/fputcsv sub-systems here's a snippet that builds an escaped CSV string from an associative array....
$output = '';
foreach ($list as $row) {
$output .= '"' . implode('", "', array_values($row)) . '"' . "\r\n";
}
Or from a list of objects...
foreach ($list as $obj) {
$output .= '"' . implode('", "', array_values((array) $obj)) . '"' . "\r\n";
}
Then you can output the string as desired.
Solution 11 - Php
Another solution is achieved using implode + preg_replace as follows:
$a = ['A', 'B', 'C'];
implode(',', preg_replace('/^(.*)$/', '"$1"', $a)); // "A","B","C"
$b = [];
implode(',', preg_replace('/^(.*)$/', '"$1"', $b)); // empty string
As you can see this also saves you from checking if the array is empty.
Solution 12 - Php
you can do it this way also
<?php
$csv= '\'' . join(array('lastname', 'email', 'phone'),'\',').'\'';
echo $csv;
?>
Solution 13 - Php
I think this is what you are trying to do
$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", explode(',', $array)) . "'";
Solution 14 - Php
Another possible option, depending on what you need the array for:
$array = array('lastname', 'email', 'phone');
echo json_encode($array);
This will put '[' and ']' around the string, which you may or may not want.
Solution 15 - Php
Don't forget to escape your data! If you want to put data in a CSV file without fputcsv function or put it in a SQL query use this code:
$comma_separated = "'" . implode("','", array_map('addslashes', $array)) . "'";
This code avoids SQL injection or data fragmentation when input array contains single quotation or backslash characters.