0

Hi I have the following mult-dimensional array:

Multi-dimensional array

I would like to group by the CustID and sum zero_days, thirty_60 ..etc

I can sum the totals as follows:

$zero_30 = array_sum(array_column($aging,'zero_30'));
$thirty_60 = array_sum(array_column($aging,'thirty_60'));
$sixty_90 = $array_sum(array_column($aging,'sixty_90'));
$ninety_plus = array_sum(array_column($aging,'ninety_plus'));

However I am not sure how I could produce an array where totals are summed by each CustID ?

user2197774
  • 453
  • 1
  • 6
  • 20

1 Answers1

0

You can try this :

$result = [];
foreach ($array as $data) {

    $custID = $data->CustID;

    // If you don't have the current CustID : you create it and add all data
    if (!isset($result[$custID])) {
        $result[$custID] = $data;
    } 
    // Now if you have one entry : you just sum the data with the one you already have
    else {
         $result[$custID]->zero_days   += $data->zero_days;
         $result[$custID]->thirty_60   += $data->thirty_60;
         $result[$custID]->sixty_90    += $data->sixty_90;
         $result[$custID]->ninety_plus += $data->ninety_plus;
    }
}
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36