Skip to content
Prev Previous commit
Next Next commit
add reduce with keys to lazy collections
  • Loading branch information
mokhosh committed Jan 10, 2021
commit 4777a5415758f05049ee12765b66f71980bc0681
18 changes: 18 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,24 @@ public function reduce(callable $callback, $initial = null)
return $result;
}

/**
* Reduce an associative collection to a single value.
*
* @param callable $callback
* @param mixed $initial
* @return mixed
*/
public function reduceWithKeys(callable $callback, $initial = null)
{
$result = $initial;

foreach($this as $key => $value) {
$result = $callback($result, $value, $key);
}

return $result;
}

/**
* Replace the collection items with the given items.
*
Expand Down