我喜欢将数据库中的数组与另一个数组进行比较,以使用isset
或array_key_exists
生成一个新的缺失id数组。这是我创建的数据库中的数组。
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
,因为这个列表可能包含也可能不包含新的和不在$local_ids
中的id。这些可以忽略,我只需要删除的
我知道一个方法是这样的
<?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);
?>