Group array data on one column and sum data from another column to form a flat associative array

So, first you need $amountsArray to get assigned the values you listed, somehow. Then:

$bankTotals = array();
foreach($amountsArray as $amount)
{
  $bankTotals[$amount['name']] += $amount['amount'];
}

Output: (Demo)

Warning: Undefined array key "Bank BRI"

Warning: Undefined array key "Bank BCA"

Warning: Undefined array key "Bank CIMB Niaga"

Warning: Undefined array key "Bank BNI"

Warning: Undefined array key "Bank Mandiri"

Warning: Undefined array key "Bank Permata"
array (
  'Bank BRI' => 34534534,
  'Bank BCA' => 1435773657,
  'Bank CIMB Niaga' => 1338303418,
  'Bank BNI' => 124124,
  'Bank Mandiri' => 0,
  'Bank Permata' => 352352353,
)

After this, $bankTotals is an array indexed on name of the bank, with the value of the total amount for the bank. You can use this array as you see fit from here.

One thing that might be useful is another foreach loop to print it all out:

foreach($bankTotals as $name => $amount)
{
  echo $name.".....".$amount."\n";
}

Leave a Comment