带回调函数的 PHP Sum 函数
我从数据库中获取一些数据结构:
object {
currency,
rate,
amount
}
我想找到一些函数,例如: $data.sum($a => $a.amount) 用于返回金额总和。但我找不到它。所以有人写了:
array_sum(array_map(function($obj) {
return $obj->amount;
}, $transferList));
有什么最简单的方法吗?
I get some data structure from database:
object {
currency,
rate,
amount
}
I want find some function like: $data.sum($a => $a.amount) for return sum of amount. But I can't find it. So a have write this:
array_sum(array_map(function($obj) {
return $obj->amount;
}, $transferList));
Is any easiest way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 array_reduce 避免函数调用,如
Or just使用
SELECT SUM()
在纯 SQL 中执行此操作You can avoid a function call using array_reduce as in
Or just do it in plain SQL with
SELECT SUM()