logo
Tags down

shadow

How to Sum values in array by key indexes in loop


By : Leapepe
Date : May 03 2020, 08:17 AM

I try  sum some values from the array from certain ranges provided by another array.

I've tried with the following function :

function sumArray($array, $min, $max) {
    $sum = 0;
    foreach ($array as $k => $v) {
       if ($k >= $min && $k <= $max) {
          $sum += $v;
       }
    }
    return $sum;
 }

and this works if i hard code the numbers,

but i can't figure out how to make this to work in a loop

So I have this array named dates

array(3) {
  ["June"]=> int(4)
  ["July"]=> int(31)
  ["August"]=> int(29)
}

and i have this  array named daily_values

array(64) {
  [0]=> int(8)
  [1]=> int(6)
  [2]=> int(10)
  [3]=> int(15)
  [4]=> int(10)
  [5]=> int(9)
  [6]=> int(9)
  .
  .
  .
  [63] => int(10)

Now I want to do here is: based on how many entries are in for example

June which is 4 in this case I want to get the sum of values in daily_values array like this:

first loop iteration will sum values in daily_values array from 0 to 3, the second loop iteration will sum values from 4 to 34 and third loop iteration will sum values from 35 to 63 and so on and so on.

and I want from all this is to have an array which will be like this

["sum of 0,3", "sum of 4,34", "sum of 35,63"]

Just a note here, this array is much bigger, but I cut it short just for question purposes.

Answer :

try the Following code :

I created a same test and it works good for me. Hope it helps you in someway.

<?php

$june = [1,1,1,1];
$july = [1,1];
$august = [1,1,1,1];

$months = array(
    'June' => $june, 
    'July' => $july, 
    'August' => $august, 
);

$daily = [1,1,1,1,5,5,10,10,10,10,2,2,2,2,2];
$sum_arr = array();

$i = 0;
foreach($months as $key=>$month){

    $month_count = $month;
    $month_sum = 0;
    while($month_count > 0){
        $month_sum += $daily[$i];
        $i++;
        $month_count--;
    }
    $sum_arr[$key] = $month_sum;
}

echo '<pre>';
print_r($sum_arr);

?>


Share : facebook icon twitter icon
Related Posts Related Posts :
  • clear array after form submit
  • pass url variable to javascript
  • access data using api
  • div position fixed css
  • add horizontal line between two div
  • disable toggle button css
  • css fixed line height
  • background image not working
  • table cell border
  • text line break css
  • overlay div on div
  • text direction rtl css
  • javascript convert string to an object
  • bind inner div click to outer div
  • jquery ajax oncomplete
  • how to use promise in for loop
  • jquery get element using variable
  • javascript send textbox value
  • pass div class content to another class or id
  • css new line after element
  • css calc not working properly
  • link disabled jquery
  • sql join table to subquery
  • MS Access SQL Issue with OR AND Operators
  • set textarea value using php
  • php merge two arrays into one
  • php form with google captcha
  • update current row in php
  • php date format mysql
  • mysql pdo select query
  • php function return an array
  • PHP mysqldump database to sql file
  • display image query
  • php create array using foreach loop
  • associative array php
  • group_concat in mysql laravel
  • php array_push empty
  • mysql on update cascade not working
  • pdo insert prepared statement
  • mysql insert ignore vs on duplicate key
  • mysql insert into existing row
  • mysql multi insert query
  • mysql group by show all rows
  • php change profile image
  • combine multiple arrays into one array php
  • how to sum values in an array
  • how to get sum of the total time
  • How to Create Facebook live stream without notification using API ?
  • on button click make div visible
  • css affect another element on hover
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org