wkilc Posted August 10, 2010 Share Posted August 10, 2010 Hi all, I'm pretty much a noob, and I've tried looking in the manual, but I don't know enough to even look in the right direction. Implode function perhaps? I'm pulling values from a MySQL database... for example: $food $drink $price If the array has a value in it , I want it to report the value and then add a separator (like a comma) between the values. If the array is empty, I don't want the orphaned comma. For example: $food = "" $drink = "beer" $price = "five dollars" Right I can print: beer five dollars even: , beer, five dollars But what I want is: beer, five dollars Thanks. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210272-adding-seperator-if-values-are-not-empty/ Share on other sites More sharing options...
sasa Posted August 10, 2010 Share Posted August 10, 2010 try <?php $food = ""; $drink = "beer"; $price = "five dollars"; $test = array($food, $drink, $price); foreach ($test as $key => $v ) if (!$v) unset ($test[$key]); $test = implode(', ', $test); echo $test; ?> Link to comment https://forums.phpfreaks.com/topic/210272-adding-seperator-if-values-are-not-empty/#findComment-1097275 Share on other sites More sharing options...
wkilc Posted August 10, 2010 Author Share Posted August 10, 2010 Perfect! Thank you. ~Wayne Link to comment https://forums.phpfreaks.com/topic/210272-adding-seperator-if-values-are-not-empty/#findComment-1097313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.