mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-17 12:28:41 +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], '" ');
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
$count = $count === 0 ? count($array) : $count;
|
||||
$sum = 0.0;
|
||||
$sum = 0.0;
|
||||
|
||||
for($i = $start; $i <= $count-1; $i++) {
|
||||
if(!isset($array[$i])) {
|
||||
for ($i = $start; $i <= $count - 1; $i++) {
|
||||
if (!isset($array[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -261,4 +278,9 @@ class ArrayUtils
|
|||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
public static function arraySumRecursive(array $array)
|
||||
{
|
||||
return array_sum(self::arrayFlatten($array));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user