Reputation: 2109
I have a form which sends lots of GET['var'] in a form.
Ive worked out how to set it to send var[1]=data&var[2]=some&var[3]=thing etc etc
but how do I catch each variable and combine it into one string, seperated with a ", "?
so $var = data, some, thing
UPDATE:
Sorry I should of mentioned I already have the function that implodes the string but I dont know how to combine all the Var[n]'s into one $var string.
Hope this makes sense!
Solved!
Thanks Kazar, your first answer actually worked a treat! it required me altering my script a little but your way certainly makes sense to me now
Upvotes: 0
Views: 2904
Reputation: 36483
The php implode function will concatenate the contents of an array together, using a string to insert between them, like so:
$var = implode(',', $_GET['var']);
An additional note, the order in which the various elements of var appear does depend on the order in the querystring, so a string could come out looking like 'var2,var0,var1'. To get round this, you may want to do a key sort first:
$var = $_GET['var'];
ksort($var);
$joinedString = implode(',', $var);
Edit: According to the question edit:
Assuming you mean to create the query string again...?
$var = $_GET['var'];
$components = array();
foreach($var as $key=>$value) {
$components[] = "var[" . $key . "]=" . $value;
}
$string = implode('&', $components);
Might help to have a more detailed description.
Upvotes: 7
Reputation: 54074
Just use the implode function that concatenates the contents of an array.
$var = implode (',', $var);
Upvotes: 2