diff --git a/lib/Doctrine/Common/Collections/ArrayCollection.php b/lib/Doctrine/Common/Collections/ArrayCollection.php index 4fb01f48b..0eef7a24e 100644 --- a/lib/Doctrine/Common/Collections/ArrayCollection.php +++ b/lib/Doctrine/Common/Collections/ArrayCollection.php @@ -5,6 +5,7 @@ use ArrayIterator; use Closure; use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; +use const ARRAY_FILTER_USE_BOTH; use function array_filter; use function array_key_exists; use function array_keys; @@ -312,7 +313,7 @@ public function map(Closure $func) */ public function filter(Closure $p) { - return $this->createFrom(array_filter($this->elements, $p)); + return $this->createFrom(array_filter($this->elements, $p, ARRAY_FILTER_USE_BOTH)); } /** diff --git a/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php b/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php index 32c7c1eb0..448946229 100644 --- a/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php +++ b/tests/Doctrine/Tests/Common/Collections/BaseCollectionTest.php @@ -64,6 +64,19 @@ public function testFilter() : void self::assertEquals([0 => 1, 2 => 3], $res->toArray()); } + public function testFilterByValueAndKey() : void + { + $this->collection->add(1); + $this->collection->add('foo'); + $this->collection->add(3); + $this->collection->add(4); + $this->collection->add(5); + $res = $this->collection->filter(static function ($v, $k) { + return is_numeric($v) && $k % 2 === 0; + }); + self::assertSame([0 => 1, 2 => 3, 4 => 5], $res->toArray()); + } + public function testFirstAndLast() : void { $this->collection->add('one');