Added array sum

This commit is contained in:
Dennis Eichhorn 2016-09-22 19:05:33 +02:00
parent 9cd45355cf
commit ff768e182f

View File

@ -246,6 +246,23 @@ class ArrayUtils
return trim($args[$key + 1], '" '); return trim($args[$key + 1], '" ');
} }
public static function arrayFlatten(array $array) : array
{
$flat = [];
$stack = array_values($array);
while ($stack) {
$value = array_shift($stack);
if (is_array($value)) {
$stack = array_merge(array_values($value), $stack);
} else {
$flat[] = $value;
}
}
return $flat;
}
public static function arraySum(array $array, int $start = 0, int $count = 0) public static function arraySum(array $array, int $start = 0, int $count = 0)
{ {
$count = $count === 0 ? count($array) : $count; $count = $count === 0 ? count($array) : $count;
@ -261,4 +278,9 @@ class ArrayUtils
return $sum; return $sum;
} }
public static function arraySumRecursive(array $array)
{
return array_sum(self::arrayFlatten($array));
}
} }