Php convert array to comma separated string with quotes

When I implode my array I get a list that looks like this:

qwerty, QTPQ, FRQO

I need to add single quotes so it looks like:

'qwerty', 'QTPQ', 'FRQO'

Can this be done using PHP?

Php convert array to comma separated string with quotes

Rizier123

57.8k16 gold badges91 silver badges142 bronze badges

asked Dec 23, 2015 at 17:11

Tom CanfarottaTom Canfarotta

6831 gold badge5 silver badges14 bronze badges

4

Use ' before and after implode()

$temp = array("abc","xyz");

$result = "'" . implode ( "', '", $temp ) . "'";

echo $result; // 'abc', 'xyz'

answered Dec 23, 2015 at 17:32

Php convert array to comma separated string with quotes

Here is another way:

$arr = ['qwerty', 'QTPQ', 'FRQO'];

$str = implode(', ', array_map(function($val){return sprintf("'%s'", $val);}, $arr));

echo $str; //'qwerty', 'QTPQ', 'FRQO'

sprintf() is a clean way of wrapping the single quotes around each item in the array

array_map() executes this for each array item and returns the updated array

implode() then turns the updated array with into a string using a comma as glue

answered Jan 14, 2018 at 20:03

It can also be as short as this:

sprintf("'%s'", implode("', '", $array))

answered Aug 17, 2020 at 14:41

Php convert array to comma separated string with quotes

Gale YaoGale Yao

831 silver badge9 bronze badges

You can set the glue to ', ' and then wrap the result in '

$res = "'" . implode ( "', '", $array ) . "'";

http://codepad.org/bkTHfkfx

answered Dec 23, 2015 at 17:14

MusaMusa

94.7k17 gold badges113 silver badges130 bronze badges

Similar to what Rizier123 said, PHP's implode method takes two arguments; the "glue" string and the "pieces" array.

so,

$str = implode(", ", $arr);

gives you the elements separated by a comma and a space, so

$str = implode("', '", $arr);

gives you the elements separated by ', '.

From there all you need to do is concatenate your list with single quotes on either end.

answered Dec 23, 2015 at 17:20

    $ids = array();
    foreach ($file as $newaarr) {
        array_push($ids, $newaarr['Identifiant']);

    }
   $ids =array_unique($ids);
    //$idAll=implode(',',$ids);

     $idAll = "'" . implode ( "', '", $ids ) . "'";

answered Nov 20, 2019 at 14:00

Php convert array to comma separated string with quotes

1

function implode_string($data, $str_starter = "'", $str_ender = "'", $str_seperator = ",") {
    if (isset($data) && $data) {
        if (is_array($data)) {
            foreach ($data as $value) {
                $str[] = $str_starter . addslashes($value) . $str_ender . $str_seperator;
            }
            return (isset($str) && $str) ? implode($str_seperator, $str) :  null;
        }
        return $str_starter . $data . $str_ender;
    }
}

answered Dec 28, 2020 at 9:15

Php convert array to comma separated string with quotes

You can use the method called 'implode' in the PHP. That will help you to concatenate all the array values with th given delimiter.

$arr = ['qwe', 'asd', 'zxc'];
echo implode(',', array_map(function($i){return "'".$i."'";}, $arr));

You output will be something like below:

'qwe','asd','zxc'

Php convert array to comma separated string with quotes

Punit Gajjar

4,5237 gold badges32 silver badges64 bronze badges

answered May 22 at 21:37

Php convert array to comma separated string with quotes