Try something like this:
$ids = array();
while ($row = mysql_fetch_assoc($result))
{
row["UserID"];
}
echo implode(", ", $ids);
Replace "UserID" with the columnname of the id in your table.
So: first you build the array, next you implode the array into a string.
Answer from huysentruitw on Stack OverflowTry something like this:
$ids = array();
while ($row = mysql_fetch_assoc($result))
{
row["UserID"];
}
echo implode(", ", $ids);
Replace "UserID" with the columnname of the id in your table.
So: first you build the array, next you implode the array into a string.
There is my solution:
SELECT GROUP_CONCAT(UserID) as string FROM Users;
For this function the delimiter is ',' by default.
You just need to add the single quotes into your implode glue string, and at the outsides of the result string:
$array = [1, 2, 3];
echo "'" . implode("','", $array) . "'";
Output:
'1','2','3'
This will work regardless of whether your array values are strings or numbers e.g.
$array = ['1', '2', '3'];
echo "'" . implode("','", $array) . "'";
Output:
'1','2','3'
Demo on 3v4l.org
If the result is an array (like you stated in your comments) then you can use array_map to convert your array of integers to an array of strings:
$arr = [1, 2, 3];
var_dump(array_map('strval', $arr));
This will result in an array of strings:
['1', '2', '3']
I'll just throw this into the ring as alternative solution:
$string = trim(json_encode(array_map('strval', $arr)), '[]');
json_encode produces the desired result, just wrapped in a [..]; simply trim off the brackets.
try this,
$arr = array(1,2,3,4,5,6,7,8,9);
$string = implode('", "', $arr);
$string = '"'.$string.'"';
echo $string;
OUTPUT :
"1", "2", "3", "4", "5", "6", "7", "8", "9"
DEMO