从另一个数组中排除一个数组的元素


Exclude elements of one array from another array

我需要从数组$objects中排除数组$tempobjects的元素。最快的方法是什么?

$objects = new MyObjects();
$tempobjects = new MyObjects();
for($i=0; $i<10; $i++) {
  $objects->addObject(new MyObject(...));
}
//...fill $tempobjects with some temporary data
$tempobjects = $objects - $tempobjects; // HOW TO DO SOMETHING LIKE THIS?

如果 $tempobjects$objects是数组(就像你的标题提到的),根据你的示例代码,它们是而不是,你可以使用函数array_diff()(用于比较值)或array_diff_key()(用于比较键)来排除元素。

这个<<p>看到,strong>短演示。