多个阵列的运算值具有相同的关键 [英] Operation value of multiple array that have the same key
本文介绍了多个阵列的运算值具有相同的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有数组的数组
array(4) {
[0]=>
array(3) {
["a"]=>float(1000)
["b"]=>float(3)
["c"]=>float(500)
}
[1]=>
array(3) {
["a"]=>float(1000)
["b"]=>float(852)
["c"]=>float(500)
}
[2]=>
array(3) {
["a"]=>float(1000)
["b"]=>float(5)
["c"]=>float(500)
}
[3]=>
array(1) {
["e"]=>float(1000)
}
}
结果将总结所有的值相同的键,所以结果应该是:
The result will sum all the value that the same keys,so result should be:
$result =
array(
"a" =>3000,
"b"=>900,
"c"=>1500,
"e"=>1000
)
任何人都可以帮我这个TODO
Anybody could help me todo this.
感谢。
推荐答案
伪:
result <- new array # array holding result
foreach entry1 in array: # iterate outer array
foreach entry2 in entry1: # iterate each inner array
if not exists result[entry2.key]: # if key is not already in result...
result[entry2.key] = 0 # ... add key and set value to zero
result[entry2.key] += value # increment result for key with value from inner array
(我会离开的实现作为一个练习OP)
这篇关于多个阵列的运算值具有相同的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文