Skip to content

Commit

Permalink
Merge branch '5.5' of https://github.com/Lapayo/framework into Lapayo…
Browse files Browse the repository at this point in the history
…-5.5
  • Loading branch information
taylorotwell committed Jan 14, 2018
2 parents 92e30c8 + d8b2eb1 commit 337e0e8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,18 @@ public function mapSpread(callable $callback)
*/
public function mapToDictionary(callable $callback)
{
$dictionary = $this->map($callback)->reduce(function ($groups, $pair) {
$groups[key($pair)][] = reset($pair);
$dictionary = [];

return $groups;
}, []);
$this->each(function ($item, $key) use (&$dictionary, $callback) {
$pair = $callback($item, $key);
$key = key($pair);
$value = reset($pair);

if (! isset($dictionary[$key])) {
$dictionary[$key] = [];
}
$dictionary[$key][] = $value;
});

return new static($dictionary);
}
Expand Down

0 comments on commit 337e0e8

Please sign in to comment.