php 多维数组 array_sum
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12838729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
multidimensional array array_sum
提问by Mr Sorbose
I have seen various posted about this question so am aware some answers to this may exist. however I am none the wiser after reading these.
我已经看到关于这个问题的各种帖子,所以我知道可能存在一些答案。然而,在阅读这些之后,我并不聪明。
I have an array that is like the following.
我有一个如下所示的数组。
[0] => Array
(
[id] => 95659865986
[invoiceNumber] => 6374324
[invoiceTitle] => Monthly
[invoiceStatus] => Paid
[accountId] => 6235218753
[totalExVat] => 158.95
[dateCreated] => 1 Apr 2012
[vatAmount] => 20.00
)
All I wish to do is do array sum on the vatAmountvalues of this array.
我想要做的就是vatAmount对这个数组的值进行数组求和。
As the following doesnt seem to be doing much.
由于以下似乎没有做太多。
(array_sum($account_invoices['vatAmount'])
采纳答案by matthias
Just a way to do it:
只是一种方法:
$sum = 0;
foreach($account_invoices as $num => $values) {
$sum += $values[ 'vatAmount' ];
}
回答by Blablaenzo
If you have PHP 5.5+ you can do this without looping or using a callback (since function calls are relatively expensive) ... just use:
如果你有 PHP 5.5+ 你可以不用循环或使用回调来做到这一点(因为函数调用相对昂贵)......只需使用:
$sum = array_sum(array_column($account_invoices, 'vatAmount'));
回答by Khior
I would use array_map to reduce the array to only what is needed. Bear in mind, this will only work with PHP 5.3 onwards.
我会使用 array_map 将数组减少到仅需要的内容。请记住,这仅适用于 PHP 5.3 及更高版本。
$total_vat = array_sum( array_map(
function($element){
return $element['vatAmount'];
},
$account_invoices));
回答by xdazz
You could use array_mapfirst to collect the vatAmoutvalue.
您可以使用array_mapfirst 来收集vatAmout值。
$sum = array_sum(array_map(function($var) {
return $var['vatAmout'];
}, $account_invoices));
回答by Faisal
You can do this using array_map()and select the vatAmountcolumn first:
您可以使用array_map()并vatAmount首先选择列来执行此操作:
$totalVatAmount = array_sum(array_map(function($account_invoices) {
return $account_invoices['vatAmount'];
}, $account_invoices));
Of course, internally this performs a double loop; it's just that you don't see it inside the code. If you use array_reduce()then you can get rid of one loop:
当然,这在内部执行了一个双循环;只是你没有在代码中看到它。如果你使用array_reduce()那么你可以摆脱一个循环:
$totalVatAmount = array_reduce($account_invoices,
function($totalAmount, $item) {
$totalAmount += $item['vatAmount'];
return $totalAmount;
},
0
);
However, if speed is the only interest, you should use foreach. Because there is no function calls used to calculate the final sum. This solution is faster than other solution.
但是,如果速度是唯一的兴趣,您应该使用foreach. 因为没有用于计算最终总和的函数调用。此解决方案比其他解决方案更快。
$totalVatAmount = 0;
foreach ($account_invoices as $item) {
$totalVatAmount += $item['vatAmount'];
}
回答by Mark Baker
A way to do this using a PHP 5.3+ anonymous function
一种使用 PHP 5.3+ 匿名函数执行此操作的方法
$account_invoices = array(
0 => array(
'id' => '95659865986',
'invoiceNumber' => '6374324',
'invoiceTitle' => 'Monthly',
'invoiceStatus' => 'Paid',
'accountId' => '6235218753',
'totalExVat' => 158.95,
'dateCreated' => '1 Apr 2012',
'vatAmount' => 20.00
),
1 => array(
'id' => '95659865987',
'invoiceNumber' => '6374325',
'invoiceTitle' => 'Monthly',
'invoiceStatus' => 'Paid',
'accountId' => '6235218753',
'totalExVat' => 208.95,
'dateCreated' => '1 May 2012',
'vatAmount' => 25.00
),
);
$sumDetail = 'vatAmount';
$totalVAT = array_reduce($account_invoices,
function($runningTotal, $record) use($sumDetail) {
$runningTotal += $record[$sumDetail];
return $runningTotal;
},
0
);
echo $totalVAT;
回答by David Clews
array_sum_multidimensional Helper function
array_sum_multidimensional 辅助函数
if (!function_exists('array_sum_multidimensional')) {
/**
* returns the sum of a element from an array of arrays
*/
function array_sum_multidimensional(array $array, $elementKey){
$sum = 0;
foreach($array as $key => $value) {
$sum += $value[ $elementKey ];
}
return $sum;
}
}
回答by Kevin Postma
Just another way to do this using array_reduce:
使用 array_reduce 执行此操作的另一种方法:
$vatMount = array_reduce($account_invoices, function($total, $value) {
return $total + $value['vatAmount'];
});
回答by RDK
Also you can do this (if you like array_sum function):
你也可以这样做(如果你喜欢 array_sum 函数):
foreach($account_invoices as $num => $values) {
$vatAmount[] = $values[ 'vatAmount' ];
}
$Total = array_sum($vatAmount);
回答by LSerni
You can't do this directly with array_sum, which would sum everything in the array.
您不能直接使用 来执行此操作array_sum,它会将数组中的所有内容相加。
You can do it with a loop:
你可以用一个循环来做到这一点:
$sum = 0;
foreach($items as $item)
$sum += $item['vatAmount'];
or you can filter the array (in this case it isn't very convenient, but if you had to calculate, say, S&H expenses plus VAT plus..., from each single item, and then sum...):
或者你可以过滤数组(在这种情况下它不是很方便,但是如果你必须计算,比如说,S&H 费用加上增值税加上......,从每个项目中,然后总结......):
// Input: an array (element #n of array of arrays), output: VAT field
function getVAT($item)
{
return $item['vatAmount'];
}
// Array with all VATs
$vats = array_map('getVAT', $items);
$sum = array_sum($vats);

