Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org [PHP] Sum of a column by key
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 11-06-2009, 04:01 AM #1 kazuni Member   Registered: May 2003 Location: Hong Kong Distribution: Android on HTC Hero Posts: 256 Rep: [PHP] Sum of a column by key Hello everyone. For example, I have an array which looks like below: PHP Code: ``` \$mycart \$mycart[0]['product'] = 'ProductA'; \$mycart[0]['quantity'] = 10; \$mycart[1]['product'] = 'ProductA'; \$mycart[1]['quantity'] = 4; \$mycart[2]['product'] = 'ProductB'; \$mycart[2]['quantity'] = 8; \$mycart[3]['product'] = 'ProductB'; \$mycart[3]['quantity'] = 8;  ``` I wanted to run a php function, or make one, that can make an array to sum up all the quantity by similar "product": PHP Code: ``` \$newcart \$newcart[0]['product'] = 'ProductA'; \$newcart[0]['quantity'] = 14; \$newcart[1]['product'] = 'ProductB'; \$newcart[1]['quantity'] = 16;  ``` is there a function in php that does this?
 11-07-2009, 09:47 PM #2 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: I don't know of a PHP function that does what you want but it should be fairly simple to create your own. I would create a result array where the key is your product and the value is the sum of the quantity then just loop through the first array incrementing the total by the value in quantity. Thus you would have something like: PHP Code: ``` foreach (\$newcart as \$product => \$quantity)   \$total[\$product] += \$quantity;  ```
11-08-2009, 11:48 PM   #3
kazuni
Member

Registered: May 2003
Location: Hong Kong
Distribution: Android on HTC Hero
Posts: 256

Original Poster
Rep:
Quote:
 Originally Posted by graemef I don't know of a PHP function that does what you want but it should be fairly simple to create your own. I would create a result array where the key is your product and the value is the sum of the quantity then just loop through the first array incrementing the total by the value in quantity. Thus you would have something like: PHP Code: ``` foreach (\$newcart as \$product => \$quantity)    \$total[\$product] += \$quantity;  ```
That's the ideal side; because I can just simply use a foreach loop to assign and add the quantity (as a value) to the product (as a key); however, I still can't think of an easy way.

anyone else got any ideas?

 11-09-2009, 12:43 AM #4 graemef Senior Member   Registered: Nov 2005 Location: Hanoi Distribution: Fedora 13, Ubuntu 10.04 Posts: 2,379 Rep: Maybe you need to be a little clearer about what you mean by "easy way". What are the problems with the solution I suggested? How wouldyou like the solution that I suggested be made easier?
 11-09-2009, 08:54 AM #5 Guttorm Senior Member   Registered: Dec 2003 Location: Trondheim, Norway Distribution: Debian and Ubuntu Posts: 1,292 Rep: Hi PHP does have a lot of array functions, but they are mostly manipulating and getting information from arrays as key+value pairs. And since your arrays are not like that, you will need to loop thru the arrays with regular code. PHP Code: ``` \$totals = array();foreach (\$mycart as \$row) {  \$product = \$row['product'];  \$quantity = \$row['quantity'];  \$totals[\$product] += \$quantity;}\$newcart = array();foreach (\$totals as \$product => \$quantity) {  \$newcart[] = array('product' => \$product, 'quantity' => \$quantity);}  ```

 Tags array, copying, php5, sum

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post RVF16 Programming 11 05-31-2009 07:16 AM cmnorton Programming 3 01-15-2009 03:12 AM cgcamal Programming 4 11-20-2008 10:43 AM konqi Programming 2 07-10-2008 05:13 AM stevec Programming 2 06-17-2008 10:37 AM

All times are GMT -5. The time now is 02:59 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -