问题
Possible Duplicate:
php group by SUM using multi dimensional array
I'm working on creating shopping-cart for a wholesale company. And i'll create also invoice later. It's same logic.
Firstly, i multiply paket (package) * paket adeti (package quantity) * fiyatı (price) And i wrote it at the end of list. Now i have to calculate vat. The main problem is we don't know vat ratios exactly before.
May be there exist 3 different vat ratios. (it depends on the products which customer selects) May be one.
I made an array of vats
$vats[] = array($row['vat'], $row['wholesaleprice'] - $wholesaleprice);
Result is like that
Array (
[0] => Array ( [0] => 18 [1] => 1,07 )
[1] => Array ( [0] => 8 [1] => 0,44 )
[2] => Array ( [0] => 8 [1] => 0,67 )
[3] => Array ( [0] => 18 [1] => 0,55 )
[4] => Array ( [0] => 18 [1] => 0,19 )
[5] => Array ( [0] => 8 [1] => 0,48 )
[6] => Array ( [0] => 18 [1] => 2,59 )
[7] => Array ( [0] => 8 [1] => 0,15 )
[8] => Array ( [0] => 18 [1] => 12,97 )
)
I have to sum vat group by ratios...
And i want to display like
VAT (%18) 136,26 TL VAT (%8) 16,90 TL VAT (%1) 9,28 TL
How can i do that in shortcut. I've check array functions. But i couldn't find anything useful.
Shopping Cart: http://i.stack.imgur.com/DDsCq.png
回答1:
Try something like this:
<?php
$data = Array (
Array ( 0 => '18', 1 => '1,07' ),
Array ( 0 => '8', 1 => '0,44' ),
Array ( 0 => '8', 1 => '0,67' ),
Array ( 0 => '18', 1 => '0,55' ),
Array ( 0 => '18', 1 => '0,19' ),
Array ( 0 => '8', 1 => '0,48' ),
Array ( 0 => '18', 1 => '2,59' ),
Array ( 0 => '8', 1 => '0,15' ),
Array ( 0 => '18', 1 => '12,97' )
);
// predefine array
$data_summ = array();
foreach ( $data as $value ) {
$data_summ[ $value[0] ] = 0;
}
foreach ( $data as $list ) {
$number = str_replace( ",", ".", $list[1] ) * 1;
$data_summ[ $list[0] ] += (float)$number;
}
?>
Output of $data_summ
:
Array
(
[8] => 1.74
[18] => 17.37
)
If I understand you correctly.
回答2:
<?php
function multiplyArray($array) {
$result = array();
foreach ($array as $value) {
$result[] = array_product(array_map('floatval', str_replace(',', '.', $value)));
}
return $result;
}
$array = array(
0 => array(0 => '18', 1 => '1,07'),
1 => array(0 => '8', 1 => '0,44'),
2 => array(0 => '8', 1 => '0,67'),
3 => array(0 => '18', 1 => '0,55'),
4 => array(0 => '18', 1 => '0,19'),
5 => array(0 => '8', 1 => '0,48'),
6 => array(0 => '18', 1 => '2,59'),
7 => array(0 => '8', 1 => '0,15'),
7 => array(0 => '18', 1 => '12,97'),
);
// Outputs
var_dump($array);
var_dump(multiplyArray($array));
var_dump(array_sum(multiplyArray($array)));
回答3:
Be careful! Last time I dealt with VAT the rules were very strict about calculating VAT on each line item in an invoice, then adding up the total VAT.
Her Majesties Customs and Excise do not accept totaling up the line items and then calculating the VAT on the total. There are two reasons for this:-
- Plain Greed -- mostly the rounding errors will fall in excise mans favour.
- Practicality -- Often single line items will be missing from a shipment, rejected or returned -- its much easier to work out the VAT to be credited if it was calculated for each invoice line in the first place.
来源:https://stackoverflow.com/questions/11633176/php-arrays-sum-group-by