Join function for php array

I have an array that I want to concatenate into a string using commas (,) separated values. Is there a function close in line to the union function in php?

+4
source share
5 answers

You can use implode .

$string = implode( ',', $array );

Similarly, you can return a string to an array with explode .

$array = explode( ',', $string );

+3
source

Listen in PHP:

 $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); 
+3
source

You can use the implode function:

 $your_array = array('a', 'b', 'c', 'd'); $string = implode(',', $your_array); echo $string; 

You will get this result:

 a,b,c,d 

Note that ' , ' were added between the elements of the array, not at the beginning and end of the line.

+3
source
+2
source

All Articles