izbryte Posted February 8, 2008 Share Posted February 8, 2008 I have an array of items that I need to print out. I have it set so that there is a comma after each item. Here's my current code: $specialties=explode(",",$row['specialties']); foreach($specialties as $fs) { echo "$fs, "; } Is there any way that I can remove that last comma? Right now it looks like this A, B, C, D, E, I'd like it to look like this A, B, C, D, E Thanks in advance for any help! Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/ Share on other sites More sharing options...
amites Posted February 8, 2008 Share Posted February 8, 2008 so you want to chop that last comma off? http://us3.php.net/manual/en/function.substr.php Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/#findComment-462100 Share on other sites More sharing options...
nethnet Posted February 8, 2008 Share Posted February 8, 2008 $specialties=explode(",",$row['specialties']); $max = count($specialties); $count = 0; foreach($specialties as $fs){ $count++; if($count == $max) echo "$fs"; else echo "$fs, "; } Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/#findComment-462101 Share on other sites More sharing options...
mikefrederick Posted February 8, 2008 Share Posted February 8, 2008 use an if statement. if it is not equal to the last $fs (I don't know how many there are) then echo "$fs, "; else echo "$fs"; Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/#findComment-462104 Share on other sites More sharing options...
kenrbnsn Posted February 8, 2008 Share Posted February 8, 2008 Why can't you just echo the contents of $row['specialties'], since that already contains a CSV string. If not, do <?php echo implode(', ',$specialties); ?> Ken Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/#findComment-462112 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 why not just move it into specialities? since it's already in csv in the db? your code: $specialties=explode(",",$row['specialties']);[code] so why break em apart to rebuild em back into csv? [code]$specialties=$row['specialties'];[code] [/code][/code][/code] Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/#findComment-462131 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.