Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i convert a array of values returned from a query to comma separated values

Tags:

arrays

php

mysql

I have a result set that is being returned using this code:

while ($row = mysql_fetch_array( $result )) {

echo "ID ".$row['v2id'];

}

this returns ID 2ID 3ID 4ID 8

how would i convert this to comma separated values and then store them in a variable?

so if i echoed out the variable, the final output would should look like 2, 3, 4, 8

like image 826
James Rand Avatar asked Jun 07 '11 08:06

James Rand


1 Answers

store all the values in an array, then join them using ", " as the glue

$values = array();

while ($row = mysql_fetch_array( $result )) {
    $values[] = $row['v2id'];
}

echo join(", ", $values);
like image 128
bumperbox Avatar answered Oct 05 '22 22:10

bumperbox