Php add double quotes to array values

I have an array like below format

$array = ["a, b, c, d"]

But I want to convert the array like below format

$array = ["a","b","c","d"]

I have already googling about this issue but can't get the desire solution yet.

Anybody help please ?

Php add double quotes to array values

asked Jan 28, 2019 at 8:21

Arafat RahmanArafat Rahman

9154 gold badges12 silver badges42 bronze badges

Try this with explode()

The explode() function breaks a string into an array.

Note: The "separator" parameter cannot be an empty string.

Note: This function is binary-safe.

$array = ["a, b, c, d"];
$array = explode(',', $array[0]);
print_r($array);

answered Jan 28, 2019 at 8:26

Php add double quotes to array values

MD. Jubair MizanMD. Jubair Mizan

1,5091 gold badge11 silver badges19 bronze badges

You can do it this way with explode:

$array = ["a, b, c, d"];
$array_1 = explode(', ', $array[0]);

dWinder

11.4k3 gold badges24 silver badges37 bronze badges

answered Jan 28, 2019 at 8:25

Php add double quotes to array values

You can explode the contents of the string at index 0 and use that as your new array. The explode function will create an array based on the delimiter specified.

$array = ["a, b, c, d"];
$array = explode(',', $array[0]);
print_r($array);

Output:

Array ( [0] => a [1] => b [2] => c [3] => d )

Here I have exploded the string in your array "a, b, c, d" based on the comma , delimiter. This means that you will get an array of elements where the comma indicates the separation between each element in the array.

answered Jan 28, 2019 at 8:24

Nick ParsonsNick Parsons

41.3k6 gold badges36 silver badges60 bronze badges

1

    Table of contents
  • How to add double quote to array values php [closed]
  • Php implode (101) with quotes
  • PHP: Using a variable inside a double quotes
  • PHP add single quotes to comma separated list
  • How convert array with double quotes for each item?

How to add double quote to array values php [closed]

Array(
        [0] => 25/1
        [1] => 10/1
     );
Array(
            [0] => "25/1"
            [1] => "10/1"
         );
$current_array = array(red, green);
$new_array = array();

$new_array  = array_map(function($rec){
//concatenate record with "" 
$result = $rec= '"'.$rec.'"';
return $result;
},$current_array);

print_r($result);
Array ( [0] => "red" [1] => "green" )

Php implode (101) with quotes

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
 lastname,email,phone
$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
 'lastname','email','phone'
$array = array('lastname', 'email', 'phone');


echo "'" . implode("','", $array) . "'";
$ids = sprintf("'%s'", implode("','", $ids ) );
function add_quotes($str) {
    return sprintf("'%s'", $str);
}

$csv =  implode(',', array_map('add_quotes', $array));
$array = array('lastname', 'email', 'phone');
$comma_separated = implode("','", $array);
$comma_separated = "'".$comma_separated."'";
$array = array('lastname', 'email', 'phone');
$comma_separated = "'".implode("','", $array)."'";
$array = array('lastname', 'email', 'phone');
foreach($array as &$value){
   $value = "'$value'";
}
$comma_separated = implode(",", $array);
$id = array(2222,3333,4444,5555,6666);
$ids = "'".implode("','",$id)."'";
$ids = sprintf("'%s'", implode("','", $id ) );
function implode_with_quotes(array $data)
{
    return sprintf("'%s'", implode("', '", $data));
}
$output = '';
foreach ($list as $row) {
  $output .= '"' . implode('", "', array_values($row)) . '"' . "\r\n";
}
foreach ($list as $obj) {
  $output .= '"' . implode('", "', array_values((array) $obj)) . '"' . "\r\n";
}
$a = ['A', 'B', 'C'];
implode(',', preg_replace('/^(.*)$/', '"$1"', $a)); // "A","B","C"
$b = [];
implode(',', preg_replace('/^(.*)$/', '"$1"', $b)); // empty string
$array = array('lastname', 'email', 'phone');
echo "'" . implode("','", explode(',', $array)) . "'";
$array = array('lastname', 'email', 'phone');
echo json_encode($array);
$comma_separated = "'" . implode("','", array_map('addslashes', $array)) . "'";
$data = 'ABC,DEF,GHI';

$str = "'".str_replace(',',"','",$data)."'";


echo $str;

PHP: Using a variable inside a double quotes

$imagebaseurl = 'support/content_editor/uploads/$name';
$imagebaseurl = 'support/content_editor/uploads/' . $name;
$imagebaseurl = "support/content_editor/uploads/{$name}";
$imagebaseurl = "support/content_editor/uploads/$name";
$varname = 'EXAMPLE';

$fulltext = "This is an $varname";
define('CONST_NAME', 'This is an example of a better %s');
define('EXAMPLE', sprintf(CONST_NAME, 'practice'));

echo EXAMPLE;

PHP add single quotes to comma separated list

qwerty, QTPQ, FRQO
'qwerty', 'QTPQ', 'FRQO'
$temp = array("abc","xyz");

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

echo $result; // 'abc', 'xyz'
$arr = ['qwerty', 'QTPQ', 'FRQO'];

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

echo $str; //'qwerty', 'QTPQ', 'FRQO'
sprintf("'%s'", implode("', '", $array))
$res = "'" . implode ( "', '", $array ) . "'";
$str = implode(", ", $arr);
$str = implode("', '", $arr);
    $ids = array();
    foreach ($file as $newaarr) {
        array_push($ids, $newaarr['Identifiant']);

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

     $idAll = "'" . implode ( "', '", $ids ) . "'";
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;
    }
}

How convert array with double quotes for each item?

Var arr=[];

arr.push(1)
arr.push(2)
arr.pus(3)
var output=["1","2","3"]
var output= arr.map(item => "'" + item + "'").join();
const input = [1, 2, 3];
const output = input.map(i => i.toString());

Next Lesson PHP Tutorial