tags:

views:

74

answers:

4

I have my array data as shown below:

Array ( 
  [0] => Array ( [name] => Bank BRI [amount] => 0 ) 
  [1] => Array ( [name] => Bank BRI [amount] => 0 ) 
  [2] => Array ( [name] => Bank BCA [amount] => 1412341234 )
  [3] => Array ( [name] => Bank CIMB Niaga [amount] => 532532552 ) 
  [4] => Array ( [name] => Bank BRI [amount] => 34534534 ) 
  [5] => Array ( [name] => Bank CIMB Niaga [amount] => 453425243 ) 
  [6] => Array ( [name] => Bank BRI [amount] => 0 ) 
  [7] => Array ( [name] => Bank BNI [amount] => 124124 ) 
  [8] => Array ( [name] => Bank CIMB Niaga [amount] => 352345623 ) 
  [9] => Array ( [name] => Bank BCA [amount] => 23432423 ) 
  [10] => Array ( [name] => Bank Mandiri [amount] => 0 ) 
  [11] => Array ( [name] => Bank BCA [amount] => 0 ) 
  [12] => Array ( [name] => Bank BCA [amount] => 0 ) 
  [13] => Array ( [name] => Bank Permata [amount] => 352352353 )
)

How to sum 'amount' based on same 'bank name'.

My data should shown like this:

Bank BRI........... with '*total amount*'
Bank BCA........... with '*total amount*'
Bank CIMB NIAGA.... with '*total amount*'
Bank BNI........... with '*total amount*'
Bank Mandiri....... with '*total amount*'
Bank Permata....... with '*total amount*'
A: 

Example in C#:

Dictionary<string,object>[] items = {
  new Dictionary<string, object> {{ "name", "Bank BRI"}, {"amount", 0 }},
  new Dictionary<string, object> {{ "name", "Bank BRI"}, {"amount", 0 }},
  new Dictionary<string, object> {{ "name", "Bank BCA"}, {"amount", 1412341234 }},
  new Dictionary<string, object> {{ "name", "Bank CIMB Niaga"}, {"amount", 532532552 }} 
};

var amounts = new Dictionary<string, int>();

foreach (var item in items) {
  string bank = (string)item["name"];
  int amount = (int)item["amount"];
  if (amounts.ContainsKey(bank)) {
    amounts[bank] += amount;
  } else {
    amounts.Add(bank, amount);
  }
}

foreach (var amount in amounts) {
  Console.WriteLine("{0}: {1}", amount.Key, amount.Value);
}
Guffa
Thanks, I'll try to implemet your script into PHP.
Chandra
my script stuck... i'm still trying to convert your C# into PHP. any idea? Thanks.
Chandra
There is no need to convert Guffa's script JGB146 response is virtually the same.
ozatomic
Solved! Thanks.
Chandra
A: 
$a = arrayofindonesianbanks;

foreach ($a as $anarrays) {
        echo "$anarrays[name]."  ".$anarrays[amount]";
    }
}

see foreach in php.

kaskus
Thanks a lot, i'm trying to convert GUFFA algorithm into PHP. but i still stuck/confused.
Chandra
Solved! Thanks.
Chandra
ok klick dont tanda oknya ahahah.
kaskus
+3  A: 

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'];
}

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";
}
JGB146
Thank you JGB146I've been scripting for almost 3 days without any result... Solved now!
Chandra
A: 
<?php

// array of bank structure
$banks = array();
$banks[] = array('name'=>'Bank BRI','amount'=>rand());
$banks[] = array('name'=>'Bank BRI','amount'=>rand());
$banks[] = array('name'=>'Bank BCA','amount'=>rand());
$banks[] = array('name'=>'Bank CIMB','amount'=>rand());
$banks[] = array('name'=>'Bank BRI','amount'=>rand());
$banks[] = array('name'=>'Bank CIMB','amount'=>rand());
$banks[] = array('name'=>'Bank BRI','amount'=>rand());
$banks[] = array('name'=>'Bank BNI','amount'=>rand());
$banks[] = array('name'=>'Bank CIMB','amount'=>rand());
$banks[] = array('name'=>'Bank BCA','amount'=>rand());
$banks[] = array('name'=>'Bank Mandiri','amount'=>rand());
$banks[] = array('name'=>'Bank BCA','amount'=>rand());
$banks[] = array('name'=>'Bank BCA','amount'=>rand());
$banks[] = array('name'=>'Bank Permata','amount'=>rand());

// begin the iteration for grouping bank name and calculate the amount
$amount = array();
foreach($banks as $bank) {
    $index = bank_exists($bank['name'], $amount);
    if ($index < 0) {
        $amount[] = $bank;
    }
    else {
        $amount[$index]['amount'] +=  $bank['amount'];
    }
}
print_r($amount); //display 

// for search if a bank has been added into $amount, returns the key (index)
function bank_exists($bankname, $array) {
    $result = -1;
    for($i=0; $i<sizeof($array); $i++) {
        if ($array[$i]['name'] == $bankname) {
            $result = $i;
            break;
        }
    }
    return $result;
}
Husni
Thank you Husni, really great code, it works.
Chandra