use Doctrine\Common\Collections\ArrayCollection; $collection = new ArrayCollection([1, 2, 3, 4, 5]); if ($collection->contains(3)) { echo 'The collection contains the value 3.'; } else { echo 'The collection does not contain the value 3.'; }
use Doctrine\Common\Collections\ArrayCollection; class User { private $id; private $name; public function __construct($id, $name) { $this->id = $id; $this->name = $name; } public function getId() { return $this->id; } } $user1 = new User(1, 'John'); $user2 = new User(2, 'Jane'); $user3 = new User(3, 'Bob'); $users = new ArrayCollection([$user1, $user2, $user3]); if ($users->contains($user2)) { echo 'The collection contains the user with ID 2.'; } else { echo 'The collection does not contain the user with ID 2.'; }This example creates an ArrayCollection with three User objects and checks if it contains the User object with an ID of 2. If it does, a message is printed to the screen saying so. Overall, the ArrayCollection class is a very useful tool for managing collections of data in PHP applications. Its contains() method allows for easy searching within a collection.