/    Sign up×
Community /Pin to ProfileBookmark

Imploding variables into comma seperated string

I have a PHP post form that posts up to 4 variables. I need whatever variables are passed to be combined into one comma seperated string (eg variable1,variable2,variable3,variable4). This i can do with the code below.

[CODE]$combo = array($one, $two, $three, $four);
$list = implode(“,”, $combo);[/CODE]

my problem is that the form fields are optional by design so that the user doesn’t always post all 4 variables. Sometimes they will post 1, sometimes they will post all 4. So if a user only selects two fields and submits the form i will end up with a string looking like:
[COLOR=”Red”]variable1,variable2,,[/COLOR]
when what i really want is (note the removal of the trailing commas at the end of the string):
[COLOR=”DarkGreen”]variable1,variable2[/COLOR]

Anyone got an idea on how i could strip any trailing commas from the end of the string?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiOct 17.2009 — Here's two options, first one is my preference and that is store the optional form elements as an array and just implode that:

[code=html]<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="1" />[/code]


[code=php]implode(',', $_POST['foo']);[/code]

Otherwise if all the commas will be at the end you can use rtrim:

[code=php]$string = rtrim($string, ',');[/code]
Copy linkTweet thisAlerts:
@phantomlimbauthorOct 17.2009 — Thanks for the reply, you're first option is interesting, i didn't think of that.

i gave it a try, but i must be doing something wrong as i get the Warning: implode() [function.implode]: Invalid arguments passed.

i set the form up as you suggested:

[CODE]<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="2" />
<input type="checkbox" name="foo[]" value="3" />
<input type="checkbox" name="foo[]" value="4" />[/CODE]


and added the new implode to the list variable.

[CODE]$list = implode(',', $_POST['foo']); [/CODE]

this is going to sound stupid, but you mentioned storing the form elements as an array, how do i go about that? Is there something i have to add to the brackets in foo[]?
Copy linkTweet thisAlerts:
@MindzaiOct 17.2009 — I assume you are sending this form via POST and not GET? If not you will need to use the $_GET superglobal instead. The following code is a complete example:

[code=php]
<?php
if (isset($_POST['foo'])) {
echo implode(',', $_POST['foo']);
}
?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="1" />
<input type="checkbox" name="foo[]" value="1" />
<button type="submit">Go</button>
</form>
[/code]


this is going to sound stupid, but you mentioned storing the form elements as an array, how do i go about that? Is there something i have to add to the brackets in foo[]?[/quote]

There's no such thing as a stupid question if you don't know the answer! The array is achieved by including square brackets in the name attributes of the elements. In the above example, you are essentially creating a [B]foo[/B] array with 4 elements which will have numerical keys (empty square brackets will be automatically sequentially keyed for you). You can also specify the index to create an associative array (or a numerically indexed array with custom indeces):

[code=html]
<input type="checkbox" name="foo[beer]" value="1" />
<input type="checkbox" name="foo[whiskey]" value="2" />
<input type="checkbox" name="foo[vodka]" value="3" />
<input type="checkbox" name="foo[lemonade]" value="4" />
[/code]


You will then have the following in $_POST (depending of which elements were provided of course):

<i>
</i>Array
(
[foo] =&gt; Array
(
[beer] =&gt; 1
[whiskey] =&gt; 1
[vodka] =&gt; 1
[lemonade] =&gt; 1
)

)


You can even go to multiple dimensions:

[code=html]
<input type="checkbox" name="foo[beer][budweiser]" value="1" />
<input type="checkbox" name="foo[beer][fosters]" value="1" />
<input type="checkbox" name="foo[beer][heineken]" value="1" />
<input type="checkbox" name="foo[whiskey]" value="1" />
<input type="checkbox" name="foo[vodka]" value="1" />
<input type="checkbox" name="foo[lemonade]" value="1" />
[/code]


<i>
</i>Array
(
[foo] =&gt; Array
(
[beer] =&gt; Array
(
[budweiser] =&gt; 1
[fosters] =&gt; 1
[heineken] =&gt; 1
)

<i> </i> [whiskey] =&gt; 1
<i> </i> [vodka] =&gt; 1
<i> </i> [lemonade] =&gt; 1
<i> </i> )

)

Copy linkTweet thisAlerts:
@phantomlimbauthorOct 17.2009 — Cheers! Thanks for the in-depth response, that works great Mindzai. Much appreciated.
×

Success!

Help @phantomlimb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...