LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 11-06-2009, 04:01 AM   #1
kazuni
Member
 
Registered: May 2003
Location: Hong Kong
Distribution: Android on HTC Hero
Posts: 256

Rep: Reputation: 30
[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?
 
Old 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: Reputation: 148Reputation: 148
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
 
Old 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: Reputation: 30
Quote:
Originally Posted by graemef View Post
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?
 
Old 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: Reputation: 148Reputation: 148
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?
 
Old 11-09-2009, 08:54 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,136

Rep: Reputation: 230Reputation: 230Reputation: 230
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);

 
  


Reply

Tags
array, copying, php5, sum


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read text file column by column RVF16 Programming 11 05-31-2009 07:16 AM
Changing Enter Key Behavior In PHP/HTML FORM cmnorton Programming 3 01-15-2009 03:12 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 10:43 AM
PHP: storing all data form a db row in variables with the same name as the db column konqi Programming 2 07-10-2008 05:13 AM
Using SSH Public Key Authentication with PHP stevec Programming 2 06-17-2008 10:37 AM


All times are GMT -5. The time now is 05:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration