dbutler Posted August 13, 2008 Share Posted August 13, 2008 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 More sharing options...
Ingolme Posted August 13, 2008 Share Posted August 13, 2008 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 More sharing options...
Jesdisciple Posted August 13, 2008 Share Posted August 13, 2008 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 More sharing options...
dbutler Posted August 13, 2008 Author Share Posted August 13, 2008 ok, I'll try the example and see if I can get it to work. Thanks! drb Link to comment Share on other sites More sharing options...
dbutler Posted August 13, 2008 Author Share Posted August 13, 2008 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 More sharing options...
boen_robot Posted August 13, 2008 Share Posted August 13, 2008 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 More sharing options...
dbutler Posted August 13, 2008 Author Share Posted August 13, 2008 That did it...thanks!!!!! drb Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.