如果数据库中存在id,则从数组中删除


Remove from array if id exists in database?

我从一个api中提取数据,因此我有一个将一些id存储到数组中的循环。

我需要做的是从数据库中选择所有id,然后从初始数组中删除数据库中找到的所有id。因此,我可以继续在api中查询当前没有的id。

为了更有意义,请查看以下内容:

$matches = $database->get_results('SELECT match_id FROM `matches` WHERE `order_id`='.$order_id);
if ($matchlist->totalGames !== 0) {
    foreach ($matchlist as $key) {
      $gameIds[] = $key->matchId;
    }
}

我需要从$gameIds中删除ID,如果它们已经存储在$matches中。

有什么想法吗?感谢

我试过:

$matches = $database->get_results('SELECT `match_id` FROM `matches` WHERE `order_id`='.$order_id);
if ($matchlist->totalGames !== 0) {
    foreach ($matchlist as $key) {
      $gameIds[] = $key->matchId;
    }
    $arr_matches = object2array($matches);
    $new_array = array_diff($arr_matches, $gameIds);
    var_dump($new_array);
}

错误:

Catchable fatal error: Object of class stdClass could not be converted to string

步骤1:将对象更改为数组

function object2array($object) 
{
    if (is_object($object)):
        foreach ($object as $key => $value):
            $array[$key] = $value;
        endforeach;
    else:
        $array = $object;
    endif;
    return $array;
}

步骤2:

$arr_matches = object2array($matches)
$new_array = array_diff($arr_matches , $gameIds);
// Will remove all elements contained in $gameIds from $arr_matches array.