Current location: Hot Scripts Forums » Programming Languages » PHP » Array sum of value by same key.


Array sum of value by same key.

Reply
  #1 (permalink)  
Old 07-19-10, 11:54 PM
neochandra neochandra is offline
New Member
 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Array sum of value by same key.

Could anyone help me??
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 will 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"

Thanks.
Reply With Quote
  #2 (permalink)  
Old 07-20-10, 02:48 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:


$totals 
= array();

foreach (
$banks AS $bank)
{
    if (!isset(
$totals[$bank['name']]))
        
$totals[$bank['name']] = 0;
    
    
$totals[$bank['name']] += $bank['amount'];

Reply With Quote
  #3 (permalink)  
Old 07-20-10, 03:18 AM
neochandra neochandra is offline
New Member
 
Join Date: Jul 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank You Nico. Great Code. it's works.
Reply With Quote
  #4 (permalink)  
Old 07-20-10, 04:23 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
You're welcome.
Reply With Quote
Reply

Bookmarks

Tags
array, key, php, sum


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help reading multi-dimensional array l3rady JavaScript 3 06-11-10 04:10 AM
Hello all, can you help me? K4ot1K Other Languages 2 01-23-09 05:23 AM
[SOLVED] Sorting multi array on key patter PHP 8 03-27-08 03:23 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 08:16 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.