Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added array_intersect_key to the Collection object - 19682 #19683

Merged
merged 2 commits into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,17 @@ public function intersect($items)
return new static(array_intersect($this->items, $this->getArrayableItems($items)));
}

/**
* Intersect the collection with the given items by key.
*
* @param mixed $items
* @return static
*/
public function intersectKey($items)
{
return new static(array_intersect_key($this->items, $this->getArrayableItems($items)));
}

/**
* Determine if the collection is empty or not.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,18 @@ public function testIntersectCollection()
$this->assertEquals(['first_word' => 'Hello'], $c->intersect(new Collection(['first_world' => 'Hello', 'last_word' => 'World']))->all());
}

public function testIntersectKeyNull()
{
$c = new Collection(['id' => 1, 'first_word' => 'Hello']);
$this->assertEquals([], $c->intersectKey(null)->all());
}

public function testIntersectKeyCollection()
{
$c = new Collection(['id' => 1, 'first_word' => 'Hello']);
$this->assertEquals(['first_word' => 'Hello'], $c->intersectKey(new Collection(['first_word' => 'Hello', 'last_word' => 'World']))->all());
}

public function testUnique()
{
$c = new Collection(['Hello', 'World', 'World']);
Expand Down