You want to use implode for this.
ie:
$commaList = implode(', ', $fruit);
There is a way to append commas without having a trailing one. You'd want to do this if you have to do some other manipulation at the same time. For example, maybe you want to quote each fruit and then separate them all by commas:
$prefix = $fruitList = '';
foreach ($fruits as $fruit)
{
$fruitList .= $prefix . '"' . $fruit . '"';
$prefix = ', ';
}
Also, if you just do it the "normal" way of appending a comma after each item (like it sounds you were doing before), and you need to trim the last one off, just do $list = rtrim($list, ', ')
. I see a lot of people unnecessarily mucking around with substr
in this situation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…