14th April 2009 - 3 minutes read time
Converting an array of information into a string is easy, but when you are doing this for insertion into a database having trailing commas is going to mess up your SQL statements.
Take the following example, which takes an array of values and converts them into a string of values. This practice is quite common in PHP database manipulation.
$values = array('one', 'two', 'three', 'four', 'five');
$string = '';
foreach ( $values as $val ) {
$string .= '"'.$val.'", ';
}
echo $string; // prints "one", "two", "three", "four", "five",
Obviously we need to strip the trailing comma from the end of this string. To do this you can use the following function.