use Doctrine\Common\Collections\ArrayCollection; $collection = new ArrayCollection(['apple', 'banana', 'orange']); $collection->removeElement('banana'); echo $collection->contains('banana') ? 'true' : 'false'; // Output: false
use Doctrine\Common\Collections\ArrayCollection; class Person { private $name; public function __construct(string $name) { $this->name = $name; } public function getName(): string { return $this->name; } } $alice = new Person('Alice'); $bob = new Person('Bob'); $charlie = new Person('Charlie'); $people = new ArrayCollection([$alice, $bob, $charlie]); $people->removeElement($bob); foreach ($people as $person) { echo $person->getName() . "\n"; }In this example, we create an ArrayCollection of Person objects. We then remove the $bob Person object from the collection using the removeElement method. Finally, we iterate over the remaining elements in the collection and output their names. Package library: Doctrine\Common\Collections