You want to use implode for this.
namely: $commaList = implode(', ', $fruit);
There is a way to add commas without trailing. You want to do this if you have to do some other manipulations at the same time. For example, perhaps you want to quote each fruit and then separate them with commas:
$prefix = $fruitList = ''; foreach ($fruits as $fruit) { $fruitList .= $prefix . '"' . $fruit . '"'; $prefix = ', '; }
Also, if you just make it the βnormalβ way to add a comma after each item (as if it sounds like you did before), and you need to trim the last one, just $list = rtrim($list, ', ') . In this situation, I see a lot of people who unnecessarily dump using substr .
ryeguy Mar 12 '10 at 19:24 2010-03-12 19:24
source share