Convert array to string

I have an array of strings and I need to build a string of values ​​separated by some symbol as a comma

$tags; 
+3
source share
6 answers

There is a simple implode function.

 $string = implode(';', $array); 
+17
source

You must use the implode function.

For example, implode(' ',$tags); will put a space between each element of the array.

+6
source

If someone does not want to use implode, so you can also use the following function:

 function my_implode($separator,$array){ $temp = ''; foreach($array as $key=>$item){ $temp .= $item; if($key != sizeof($array)-1){ $temp .= $separator ; } }//end of the foreach loop return $temp; }//end of the function $array = array("One", "Two", "Three","Four"); $str = my_implode('-',$array); echo $str; 
+1
source

There is also a join function which is an alias of implode.

0
source

Blast use

 $array_items = ['one','two','three','four']; $string_from_array = implode(',', $array_items); echo $string_from_array; //output: one,two,three,four 

Using join (alias implode)

 $array_items = ['one','two','three','four']; $string_from_array = join(',', $array_items); echo $string_from_array; //output: one,two,three,four 
0
source

Source: https://habr.com/ru/post/1342876/


All Articles