use Doctrine\Common\Collections\ArrayCollection; // Create an empty ArrayCollection $collection = new ArrayCollection(); // Adding elements to ArrayCollection $collection->add('apple'); $collection->add('banana'); $collection->add('orange'); $collection->add('grape'); // Outputting the ArrayCollection elements var_dump($collection->toArray());
use Doctrine\Common\Collections\ArrayCollection; // Create an ArrayCollection from an array $collection = new ArrayCollection(['apple', 'banana', 'orange', 'grape']); // Filter the ArrayCollection by keeping only fruits containing the letter 'a' $result = $collection->filter(function($fruit) { return strpos($fruit, 'a') !== false; }); // Outputting the filtered ArrayCollection elements var_dump($result->toArray());
use Doctrine\Common\Collections\ArrayCollection; // Create an ArrayCollection from an array $collection = new ArrayCollection(['apple', 'banana', 'orange', 'grape']); // Sorting the ArrayCollection alphabetically $collection->sort(); // Outputting the sorted ArrayCollection elements var_dump($collection->toArray());The Doctrine\Common\Collections package library provides a collection of classes and interfaces that is independent of the database state management layer provided by Doctrine ORM or ODM. It can be used in any PHP project where a more advanced implementation of arrays is needed.