Skip to content

Commit

Permalink
Added access to inner key on map() Closure
Browse files Browse the repository at this point in the history
This enhancement gives to map() Closure the capability to access also the keys of the inner
array holding the elements. Until now, we were able to access just the element on map Closure
but not the key. So I propose to add the ability to access also the key for do some statements
inside the Closure with key related.

I have the need to add it since in my project I must get the asociative key to take a decision
in function of the current key element and I thought that may be usefull for everyone in some
other projects to access the key of current processing element.
  • Loading branch information
didix16 authored and 1193823 committed Nov 16, 2019
1 parent 8b4de7e commit ef09245
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/Collections/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ public function getIterator() : Traversable
* @return static
*
* @psalm-template U
* @psalm-param Closure(T=):U $func
* @psalm-param Closure(T=, TKey=):U $func
* @psalm-return static<TKey, U>
*/
public function map(Closure $func) : Collection
{
return $this->createFrom(array_map($func, $this->elements));
return $this->createFrom(array_map($func, $this->elements, array_keys($this->elements)));
}

/**
Expand Down

0 comments on commit ef09245

Please sign in to comment.