PHP对某个索引相同的多维数组值求和
问题描述
这里只是一个简单的问题.我有以下数组:
just a simple problem here. I have the following array:
减少到 21 个元素.
which goes down to 21 elements.
我的目标是根据每个飞行员的团队总结所有分数(punti"索引).所以要获得这样的东西:
My goal is to sum up all the points ("punti" indexes), based on each pilot's team. So to obtain something like this:
我知道这很容易,但是解决此类问题的最快/最方便的方法是什么?
I know this is quite easy, but what is the quickest / most convenient way to solve such problem?
推荐答案
你需要迭代你的结果,当你遇到一个新团队时在输出中添加一个新条目,或者当你找到同一个团队时更新积分值再次.这最容易通过最初按团队名称索引输出,然后使用 array_values
以数字方式重新索引数组:
You need to iterate over your results, adding a new entry to the output when you encounter a new team, or updating the points value when you find the same team again. This is most easily done by initially indexing the output by the team name, and then using array_values
to re-index the array numerically:
输出(用于您的样本数据):
Output (for your sample data):
3v4l.org 上的演示
这篇关于PHP对某个索引相同的多维数组值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!