4

I have an array of strings and I need to build a string of values separated by some character like comma

$tags; 
0

6 Answers 6

21

implode()

Sign up to request clarification or add additional context in comments.

Comments

18

There is a simple function called implode.

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

Comments

7

You should use the implode function.

For example, implode(' ',$tags); will place a space between each item in the array.

Comments

2

If any one do not want to use implode so you can also use 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; 

Comments

0

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

Comments

0

Using implode

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

Using join (alias of implode)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.