use Doctrine\Common\Collections\ArrayCollection; $users = new ArrayCollection([ ['name' => 'John', 'age' => 25], ['name' => 'Jane', 'age' => 30], ['name' => 'Mike', 'age' => 21], ]); // Filter by age greater than or equal to 25 $filtered = $users->filter(function($user) { return $user['age'] >= 25; }); var_dump($filtered->toArray());
use Doctrine\Common\Collections\ArrayCollection; $fruits = new ArrayCollection(['apple', 'banana', 'cherry', 'date']); // Filter by fruits containing the letter 'a' $filtered = $fruits->filter(function($fruit) { return strpos($fruit, 'a') !== false; }); var_dump($filtered->toArray());In this example, we create an ArrayCollection containing four fruit names. We then use the filter method to create a new ArrayCollection containing only the fruits that contain the letter 'a'. The result is then converted to an array and printed using var_dump. Overall, the ArrayCollection filter is a powerful tool that can help developers quickly and easily manipulate arrays in PHP.