use Doctrine\Common\Collections\ArrayCollection; $collection = new ArrayCollection(['apple', 'orange', 'banana']); $collection->remove('orange'); print_r($collection->toArray());
Array ( [0] => apple [1] => banana )
use Doctrine\Common\Collections\ArrayCollection; $collection = new ArrayCollection(['apple', 'orange', 'banana']); $key = array_search('orange', $collection->toArray()); if (false !== $key) { $collection->remove($key); } print_r($collection->toArray());
Array ( [0] => apple [1] => banana )This example removes the 'orange' element from the collection based on its key value. In conclusion, the Collection remove method is a useful function for removing elements from a collection instance. It is part of the Doctrine\Common\Collections package library.