Php SQL中的值数组,显示哪些存在,哪些不存在';T

Php SQL中的值数组,显示哪些存在,哪些不存在';T,php,sql,mariadb,Php,Sql,Mariadb,我想不出一个标题,我知道这不好 基本上,我将有一个数组的值发布给我。这些值将是整数 那么,假设是1,2,3,4,5等等。。我已经想出了如何从数据库中获取它们各自的值,就像这样 $values = explode(",", $_GET['id']); $placeholders = str_repeat('?, ', count($values) - 1) . '?'; $CheckQuery = $database->prepare("SELECT * FROM users W

我想不出一个标题,我知道这不好

基本上,我将有一个数组的值发布给我。这些值将是整数

那么,假设是1,2,3,4,5等等。。我已经想出了如何从数据库中获取它们各自的值,就像这样

  $values = explode(",", $_GET['id']);
  $placeholders = str_repeat('?, ', count($values) - 1) . '?';
  $CheckQuery = $database->prepare("SELECT * FROM users WHERE the_id IN($placeholders)");
  $CheckQuery->execute($values);
  $Res = $CheckQuery->fetchAll(PDO::FETCH_ASSOC);
现在,这很好,因为给定我希望能够返回的ID:

ID1:0or1
ID2:0or1

但我一直在想如何返回数据库中不存在的ID。这里有什么帮助吗?

如果需要1或0,可以使用
$values
数组的结果和数据库
$Res
中的数据,创建键入这些列表的数组(1代表
$Res
和0代表
$values
),然后用数据库中找到的1覆盖0

$present = array_fill_keys(array_column($Res, 'the_id'), 1);
$allValues = array_fill_keys($values, 0);
$result = array_replace($allValues, $present);
用一些测试数据

$_GET['id'] = '1,2,3';
$values = explode(",", $_GET['id']);

$Res = [ ['the_id' => 1], ['the_id' => 3]];

$present = array_fill_keys(array_column($Res, 'the_id'), 1);
$allValues = array_fill_keys($values, 0);
$result = array_replace($allValues, $present);

print_r($result);
你得到

Array
(
    [1] => 1
    [2] => 0
    [3] => 1
)

使用
array\u diff
。这是否回答了您的问题?
$values = explode(",", $_GET['id']);
$placeholders = str_repeat('?, ', count($values) - 1) . '?';
// select `the_id` as you don't need other fields in select
$CheckQuery = $database->prepare("SELECT the_id FROM users WHERE the_id IN($placeholders)");
$CheckQuery->execute($values);

// Use `FETCH_COLUMN` to fetch ids as array:
$ids = $CheckQuery->fetchAll(PDO::FETCH_COLUMN, 0);
// Now you can use `array_diff` to get ids 
// that are in `$values` and not in `$ids`
print_r(array_diff($values, $ids));