[PHP] Sum of a column by key
Hello everyone.
For example, I have an array which looks like below: PHP Code:
PHP Code:
|
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:
|
Quote:
anyone else got any ideas? |
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? |
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:
|
All times are GMT -5. The time now is 01:59 PM. |