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! Quote 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 Quote 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, "; } Quote 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"; Quote 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 Quote 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] Quote Link to comment https://forums.phpfreaks.com/topic/90124-solved-foreach-question/#findComment-462131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.