Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marufalom's avatar

Remove Items if exists in another collection

I've two collection.

$collection1  = [
    'id' => 1, 
    'id' => 2,
    'id' => 3
];

$collection2  = [
    'id' => 7, 
    'id' => 2,
    'id' => 4
];

I want to remove items from $collection1 if an item is exists in $collection2.

In that case, item with id 2 would be removed.

0 likes
3 replies
munazzil's avatar

Something like below check,

        $items = Items::where('$collection1->id', '!=', '$collection2->id')->get();

Please or to participate in this conversation.