//input array
$array = {
2 => 5,
4 => 1,
8 => 3,
9 => 2,
}
want sum of key for a specific period like 1-5, 6-10
need output like array {1-5 => 6 ,6-10 => 5}
without using foreah
//input array
$array = {
2 => 5,
4 => 1,
8 => 3,
9 => 2,
}
want sum of key for a specific period like 1-5, 6-10
need output like array {1-5 => 6 ,6-10 => 5}
without using foreah
Try this code.. Not a rocket science..
function sumArray($array, $min, $max) {
$sum = 0;
foreach ($array as $k => $a) {
if ($k >= $min && $k <= $max) {
$sum += $a;
}
}
return $sum;
}
echo sumArray($array, 1, 5);