diff --git a/PhpCollection.php b/PhpCollection.php index 78a1bf7..f870306 100644 --- a/PhpCollection.php +++ b/PhpCollection.php @@ -498,7 +498,7 @@ public function unique() /** * @return number */ - public function sum($sKey = null) + public function sum($keyOrFunc = null) { $nResult = 0; if ($sKey === null) { @@ -506,8 +506,14 @@ public function sum($sKey = null) $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]; + } } }