Given an array of ids $galleries = array(1,2,5)
I want to have a SQL query that uses the values of the array in its WHERE clause like:
Safe way without PDO:
$ids = array_filter(array_unique(array_map('intval', (array)$ids)));
if ($ids) {
$query = 'SELECT * FROM `galleries` WHERE `id` IN ('.implode(',', $ids).');';
}
(array)$ids
Cast $ids
variable to arrayarray_map
Transform all array values into integersarray_unique
Remove repeated valuesarray_filter
Remove zero valuesimplode
Join all values to IN selection