0

I have an array of rows which need to be batched into groups of ten rows then their values summed.

Sample array with 13 rows:

[
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
    ['amount_paid' => 2050.00],
]

Desired result from sample array:

[20500.0, 8200.0]

The above represents the sum of the first 10 rows, then the sum of the remaining 3 rows.

What I tried is only for first 10 and I can't think of how to handle the data for every 10 with dynamic number of rows.

for ($i = 0; $i < count($json); $i++) {
    if ($i < 10) {
        $subtotalamount += floatval($json[$i]['amount_paid']);
    }
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Martin
  • 365
  • 4
  • 7
  • 22
  • What do you want to do with the subtotals? – Tim Biegeleisen Oct 10 '17 at 07:03
  • I will use it in a receipt system the receipt will only have 10 items each then the next will be printed in next page thus i need to sum for every page of the receipt @TimBiegeleisen. The way I make it is I used `%` to make sure that each will only have 10 but I cant use it to sum the amounts also the array I used is the same it has other elements like name of the item but i didnt include because it will be long – Martin Oct 10 '17 at 07:04
  • Do you mean, you need array having multiple value and each consist of sum of 10 values from above array? e.g. array([0]=>20500, [1]=> 8200) – Ashish Rana Oct 10 '17 at 07:06
  • @AshishRana If I can do that then maybe I can work something out with that solution can you show me that solution? – Martin Oct 10 '17 at 07:07
  • you want total of all or sum of each 10 index? – Mohideen bin Mohammed Oct 10 '17 at 07:07
  • @MohideenibnMohammed yes as i stated in the OP – Martin Oct 10 '17 at 07:09

6 Answers6

2

Easy to understand,You can use array_chunk(),array_sum(),array_column() like below:-

$new_array = array_chunk($array,10);
$sub_total_array = [];
foreach($new_array as $array){
   $sub_total_array[] = array_sum(array_column($array,'amount_paid'));
}
print_r($sub_total_array);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

Short solution with range, array_slice and array_column functions:

// $arr is your initial array
$sub_totals = [];
foreach(range(0, count($arr), 10) as $k){
    $sub_totals[] = array_sum(array_column(array_slice($arr, $k, 10), 'amount_paid'));
}

  • range(0, count($arr), 10) - generate an array containing a range of elements. It would be as [0, 10, 20]. Those elements are boundaries for each 10-sized sequence

  • array_slice($arr, $k, 10) - each next boundary $k is used for extracting next 10-sized sequence/slice from the initial array $arr

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0
$ret = array();
$subtotalamount = 0;
for ($i = 0; $i < count($json); $i++) {
    $subtotalamount += floatval($json[$i]['amount_paid']);
    if ($i % 10 == 9) {
        $ret[] = $subtotalamount;
        $subtotalamount = 0;
    }
}
if ($subtotalamount > 0) {
    $ret[] = $subtotalamount;
}
iamnoten
  • 231
  • 2
  • 7
0

try this,

$sumofall = array();
for ($i = 0; $i < count($json); $i++) {
    if ($i % 10 == 0) {       
        $sumofeach = 0 
        for ($j=$i-10;$j<$i-1;$j++) {
            $sumofeach += floatval($json[$i]['amount_paid']);
        $sumofall[] = $sumofeach
    }
}
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0
$a = array (0 => array ( 'amount_paid' => 2050.00 ), 1 => array ( 'amount_paid' => 2050.00 ) , 2 => array ( 'amount_paid' => 2050.00 ) , 3 => array ( 'amount_paid' => 2050.00 ) , 4 => array ( 'amount_paid' => 2050.00 ) , 5 => array ( 'amount_paid' => 2050.00 ) , 6 => array ( 'amount_paid' => 2050.00 ) , 7 => array ( 'amount_paid' => 2050.00 ) , 8 => array ( 'amount_paid' => 2050.00 ) , 9 => array ( 'amount_paid' => 2050.00 ) , 10 => array ( 'amount_paid' => 2050.00 ) , 11 => array ( 'amount_paid' => 2050.00 ) , 12 => array ( 'amount_paid' => 2050.00 ) , 13 => array ( 'amount_paid' => 2050.00 ) ) ; 

$b = array_column($a,'amount_paid');

$c = array_chunk($b,10);

$sum = [];
foreach($c as $arr){
    $sum[] = array_sum($arr);
}

echo "sum = ";
print_r($sum);
Ashish Rana
  • 157
  • 1
  • 10
0

The functional-style equivalent of @Anant's answer is to use array_map().

Code: (Demo)

var_export(
    array_map(
        fn($set) => array_sum(array_column($set, 'amount_paid')),
        array_chunk($array, 10)
    )
);

In a classic loop, access the column values and push the summed values into their respective groups using the truncated dividend when dividing the first level indexes by 10.

Code: (Demo)

$result = [];
foreach ($array as $i => $row) {
    $group = intdiv($i, 10);
    $result[$group] = ($result[$group] ?? 0) + $row['amount_paid'];
}
var_export($result);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136