LinuxQuestions.org
Help answer threads with 0 replies.
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

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 11-06-2009, 05:01 AM   #1
kazuni
Member
 
Registered: May 2003
Location: Hong Kong
Distribution: Android on HTC Hero
Posts: 255
Thanked: 0
[PHP] Sum of a column by key


[Log in to get rid of this advertisement]
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?
windows_xp_2003 kazuni is offline  
Tag This Post , , , ,
Reply With Quote
Old 11-07-2009, 10:47 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 11
Posts: 1,860
Thanked: 26
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
linuxfedora graemef is offline     Reply With Quote
Old 11-09-2009, 12:48 AM   #3
kazuni
Member
 
Registered: May 2003
Location: Hong Kong
Distribution: Android on HTC Hero
Posts: 255
Thanked: 0

Original Poster
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?
windows_98_nt_2000 kazuni is offline     Reply With Quote
Old 11-09-2009, 01:43 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 11
Posts: 1,860
Thanked: 26
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?
linuxfedora graemef is offline     Reply With Quote
Old 11-09-2009, 09:54 AM   #5
Guttorm
Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 597
Thanked: 31
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);

windows_xp_2003 Guttorm is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 08:16 AM
Changing Enter Key Behavior In PHP/HTML FORM cmnorton Programming 3 01-15-2009 04:12 AM
Concatenate column 1 and column 2 of related lines cgcamal Programming 4 11-20-2008 11: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 06:13 AM
Using SSH Public Key Authentication with PHP stevec Programming 2 06-17-2008 11:37 AM


All times are GMT -5. The time now is 07:39 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration