0

我喜欢将数据库中的一个数组与另一个数组进行比较,以使用issetor生成一个缺少 id 的新数组array_key_exists。这是我正在创建的数据库中的数组。

foreach ($listings as $listing) {
    $local_ids[$listing['id']] = 'true';
}

$local_ids = array(
    '567' => true,
    '568' => true,
    '569' => true,
    '570' => true,
    '571' => true,
    '572' => true,
    '573' => true
);

$new_ids = array(
    '568',
    '569',
    '572',
    '573',
    '574',
    '575',
    '576',
    '577'
);

取上面两个数组,我想循环遍历它们,给我一个567, 570, 571已被删除的结果。

我更改了 ,$new_ids因为此列表可能包含也可能不包含新的 ID 并且不在$local_ids. 这些可以忽略,我只需要删除那些。

4

1 回答 1

0

我知道这样的方法

<?php
$a1 = array("a" => "one", "two", "three", "four");
$a2 = array("b" => "one", "two", "three");
$result = array_diff($array1, $array2);

print_r($result);
?>
or

<?php
$a1 = array("a" => "one", "b" => "two", "c" => "three", "four");
$a2 = array("a" => "one", "two", "three");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
于 2013-05-19T15:03:11.253 回答