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

Add collapseWithKeys to Collection #52347

Merged
merged 1 commit into from
Aug 1, 2024
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
22 changes: 22 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,28 @@ public function collapse()
return new static(Arr::collapse($this->items));
}

/**
* Collapse the collection of items into a single array while preserving its keys.
*
* @return static<mixed, mixed>
*/
public function collapseWithKeys()
{
$results = [];

foreach ($this->items as $key => $values) {
if ($values instanceof Collection) {
$values = $values->all();
} elseif (! is_array($values)) {
continue;
}

$results[$key] = $values;
}

return new static(array_replace(...$results));
}

/**
* Determine if an item exists in the collection.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ public function collapse()
});
}

/**
* Collapse the collection of items into a single array while preserving its keys.
*
* @return static<mixed, mixed>
*/
public function collapseWithKeys()
{
return new static(function () {
foreach ($this as $values) {
if (is_array($values) || $values instanceof Enumerable) {
foreach ($values as $key => $value) {
yield $key => $value;
}
}
}
});
}

/**
* Determine if an item exists in the enumerable.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,20 @@ public function testCollapseWithNestedCollections($collection)
$this->assertEquals([1, 2, 3, 4, 5, 6], $data->collapse()->all());
}

#[DataProvider('collectionClassProvider')]
public function testCollapseWithKeys($collection)
{
$data = new $collection([[1 => 'a'], [3 => 'c'], [2 => 'b'], 'drop']);
$this->assertEquals([1 => 'a', 3 => 'c', 2 => 'b'], $data->collapseWithKeys()->all());
}

#[DataProvider('collectionClassProvider')]
public function testCollapseWithKeysOnNestedCollections($collection)
{
$data = new $collection([new $collection(['a' => '1a', 'b' => '1b']), new $collection(['b' => '2b', 'c' => '2c']), 'drop']);
$this->assertEquals(['a' => '1a', 'b' => '2b', 'c' => '2c'], $data->collapseWithKeys()->all());
}

#[DataProvider('collectionClassProvider')]
public function testJoin($collection)
{
Expand Down