$color is array
$sql=" SELECT * FROM products WHERE color IN (".implode(',', $color).")";
its showing result is
SELECT * FROM products WHERE color IN (red,green,blue);
SELECT * FROM products WHERE color IN ('red','green','blue');
While using ' around implode and "','" as a glue works like a charm in this situation you can also use array_map to surround each value of the array with ' and then implode with simple ,
$color = ['red', 'green', 'blue'];
$string = implode(
',',
array_map(
function ($value) {
return "'{$value}'";
},
$color
)
);
echo $string; // => 'red','green','blue'
It might be overkill in this case and it may be a little slower than other answers.
'".implode("','", $color)."';// output 'red','green','blue'