Klausos Klausos
Klausos Klausos

Reputation: 16040

Exclude elements of one array from another array

I need to exclude elements of the array $tempobjects from the array $objects. What is the quickest way to do this?

$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?

Upvotes: 1

Views: 466

Answers (1)

gkalpak
gkalpak

Reputation: 48212

If $tempobjects and $objects were arrays (like your title mentions), which based on your sample code they are not, you could exclude elements using functions array_diff() (for comparing values) or array_diff_key() (for comparing keys).

See, also, this short demo.

Upvotes: 2

Related Questions