Skip to content

Commit 98ff41c

Browse files
VinceGtaylorotwell
authored andcommitted
Added array_intersect_key to the Collection object - 19682 (#19683)
* Added array_intersect_key to the Collection object - 19680 * style ci
1 parent 5628796 commit 98ff41c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/Illuminate/Support/Collection.php

+11
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,17 @@ public function intersect($items)
665665
return new static(array_intersect($this->items, $this->getArrayableItems($items)));
666666
}
667667

668+
/**
669+
* Intersect the collection with the given items by key.
670+
*
671+
* @param mixed $items
672+
* @return static
673+
*/
674+
public function intersectKey($items)
675+
{
676+
return new static(array_intersect_key($this->items, $this->getArrayableItems($items)));
677+
}
678+
668679
/**
669680
* Determine if the collection is empty or not.
670681
*

tests/Support/SupportCollectionTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,18 @@ public function testIntersectCollection()
590590
$this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Collection(['first_world' => 'Hello', 'last_word' => 'World']))->all());
591591
}
592592

593+
public function testIntersectKeyNull()
594+
{
595+
$c = new Collection(['id' => 1, 'first_word' => 'Hello']);
596+
$this->assertEquals([], $c->intersectKey(null)->all());
597+
}
598+
599+
public function testIntersectKeyCollection()
600+
{
601+
$c = new Collection(['id' => 1, 'first_word' => 'Hello']);
602+
$this->assertEquals(['first_word' => 'Hello'], $c->intersectKey(new Collection(['first_word' => 'Hello', 'last_word' => 'World']))->all());
603+
}
604+
593605
public function testUnique()
594606
{
595607
$c = new Collection(['Hello', 'World', 'World']);

0 commit comments

Comments
 (0)