Jump to content

Array contents => variable


dbutler

Recommended Posts

Hi all, I'm looking for some code examples. I want to take the values out of an array, list them, and then assign the total contents into a variable. In other words:Take $glid[] contents (the $value in the $key => $value pair), comma seperate the values, and then assign the string of those values to a variable called $glidlisthope this makes sense, any examples?? Thanks!

Link to comment
Share on other sites

Edit: In fact, I might have mixed up Javascript's for with PHP's foreach.Use the method in Jesdisciple's post------------If I understand correctly, you want something like this:

$glidlist = "";for ($X in $glid) {  $glidlist .= ", " . $X;}

Link to comment
Share on other sites

This is untested. Note that you don't need the $key => part.

$glid = array('one', 'two', 'three');$glidlist = '';foreach($glid as $value){	$glidlist .= "$value, ";}

You could also just do this:

$glid = array('one', 'two', 'three');$glidlist = implode($glid, ', ');

Both of these examples use a comma and a space between adjacent values, but the first one also has a comma and a space at the end - unlike the second.

Link to comment
Share on other sites

Ok, I like the simplicity of the IMPLODE function, so I tried that. See below code (sorry, I changed the variable from glid to priority...long story). I have the priority being posted from a multiple select field in a form. I tried the below but the echo output is blank. Thoughts??

$priority = array($_POST['priority[]']);$prilist = implode(",",$priority);echo "	<p>$prilist</p></body>\n	</html>";

Link to comment
Share on other sites

Ok, I like the simplicity of the IMPLODE function, so I tried that. See below code (sorry, I changed the variable from glid to priority...long story). I have the priority being posted from a multiple select field in a form. I tried the below but the echo output is blank. Thoughts??
$priority = array($_POST['priority[]']);$prilist = implode(",",$priority);echo "	<p>$prilist</p></body>\n	</html>";

There is no "priority[]" key in POST. What you really want is the whole priority[] array, i.e.
$priority = $_POST['priority'];

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...