Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing [ ] From Values

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
I'm submitting multiple values from a select box, then retrieving them as a comma-separated list but, short of using str_replace, is there a better way to remove the [] that each value has?

I was expecting values like: Mon,Tue,Fri but it is giving Mon[],Tue[],Fri[].

PHP:
if(isset($_POST['DaysAvail'])) {
	$DaysSelected = array();
	foreach($_POST['DaysAvail'] as $val) {
		$DaysSelected[] = (string) $val;
	}
	$DaysSelected = implode(',', $DaysSelected);
	echo "Day values are :- ".''.$DaysSelected;
	exit;
}
 
Assuming your select box html looks something like this:

Code:
<select name="daysavail[b][COLOR=#A40000][][/color][/b]" size="5" multiple="multiple">

Then your values are already an array, and you can implode them directly i.e:

Code:
$DaysSelected = implode(',', $_POST['DaysAvail']);

Whatever else is coming along in your values is part of your selects HTML. for instance if you had a line break in the option html, or the actual square brackets there.
You need to check your HTML to see what your select box looks like.

Basically the brackets aren't something PHP is doing.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Yes, I have the brackets in the select daysavail[] and I had already tried the simple implode but it did the same thing, which is why I added the array loop. Just to be sure, I tried your suggestion again but got Mon[],Tue[],Fri[] again.
 
Odd, I thought I had done so but I don't see if in my original post!

Here is the select:

HTML:
<select name="DaysAvail[]" size="3" multiple="multiple">
<option value="">Hold CTRL for multiple selections</option>
<option value="All[]">All</option>
<option value="Mon[]">Monday</option>
<option value="Tue[]">Tuesday</option>
<option value="Wed[]">Wednesday</option>
<option value="Thu[]">Thursday</option>
<option value="Fri[]">Friday</option>
<option value="Sat[]">Saturday</option>
<option value="Sun[]">Sunday</option>
<option value="Lim[]">Limited</option>
</select>
 
Just as I pressed Submit Post, I noticed the [] after the values but I'm not sure where they come from. What I posted was from the browser's source but the [] are not in my code.
 
well! there's the answer anyway. either something in your code or something in your framework is adding the brackets to the values.
 
No framework involved here, this is manual programming but, yes, that was the answer. It was a leftover from an early attempt to make the selectbox values SELECTED from the string in the field. It is all working now as it should. Thank you.
 
Glad you figured it out.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top