mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-10 14:08:40 +00:00
Added array sum
This commit is contained in:
parent
9cd45355cf
commit
ff768e182f
|
|
@ -246,13 +246,30 @@ 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;
|
||||||
$sum = 0.0;
|
$sum = 0.0;
|
||||||
|
|
||||||
for($i = $start; $i <= $count-1; $i++) {
|
for ($i = $start; $i <= $count - 1; $i++) {
|
||||||
if(!isset($array[$i])) {
|
if (!isset($array[$i])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -261,4 +278,9 @@ class ArrayUtils
|
||||||
|
|
||||||
return $sum;
|
return $sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function arraySumRecursive(array $array)
|
||||||
|
{
|
||||||
|
return array_sum(self::arrayFlatten($array));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user