Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP add single quotes to comma separated list

Tags:

arrays

string

php

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?

like image 577
Tom Canfarotta Avatar asked Dec 23 '15 17:12

Tom Canfarotta


People also ask

How to add single quotes to comma separated string Php?

$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.


1 Answers

Use ' before and after implode()

$temp = array("abc","xyz");  $result = "'" . implode ( "', '", $temp ) . "'";  echo $result; // 'abc', 'xyz' 
like image 149
Ypages Onine Avatar answered Sep 18 '22 21:09

Ypages Onine