Skip to content

Commit

Permalink
Update PhpCollection.php
Browse files Browse the repository at this point in the history
  • Loading branch information
izica authored Oct 27, 2019
1 parent f82347c commit f726517
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions PhpCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,16 +498,22 @@ public function unique()
/**
* @return number
*/
public function sum($sKey = null)
public function sum($keyOrFunc = null)
{
$nResult = 0;
if ($sKey === null) {
foreach ($this->arCollection as $nItem) {
$nResult += $nItem;
}
} else {
foreach ($this->arCollection as $arItem) {
$nResult += $arItem[$sKey];
if (is_callable($keyOrFunc)) {
foreach ($this->arCollection as $arItem) {
$nResult += $keyOrFunc($arItem);
}
} else {
foreach ($this->arCollection as $arItem) {
$nResult += $arItem[$sKey];
}
}
}

Expand Down

0 comments on commit f726517

Please sign in to comment.