use Doctrine\Common\Collections\ArrayCollection; $collection = new ArrayCollection(); $collection->add('object1'); $collection->add('object2');
$collection->removeElement('object1');
foreach ($collection as $object) { // do something with $object }This code demonstrates how to iterate over a collection using a foreach loop. Overall, the Collection class is a useful tool for working with collections of objects in PHP. It is included in the doctrine/common library, which is a part of the Doctrine project for PHP.