我有两个存储 x 和 y 坐标的多维数组,我试图将它们合并到一个数组中,同时保留 x 值但将 y 值相加.
I have two multidimensional arrays which store x and y coordinates I am trying to merge together into a single array while preserving the x value but adding together the y values.
数组 1:
Array(
[0] => Array
(
[x] => 1327449600000
[y] => 5
)
[1] => Array
(
[x] => 1327450500000
[y] => 1
)
数组 2:
Array(
[0] => Array
(
[x] => 1327449600000
[y] => 1
)
[1] => Array
(
[x] => 1327450500000
[y] => 3
)
所以综合结果是:
Array(
[0] => Array
(
[x] => 1327449600000
[y] => 6
)
[1] => Array
(
[x] => 1327450500000
[y] => 4
)
任何帮助将不胜感激.
function add_array($a1, $a2) {
$c = count($a1);
for ($i=0;$i<$c;$i++) {
if (isset($a2[$i]) && isset($a2[$i]['y'])) {
$a1[$i]['y'] += $a2[$i]['y'];
}
}
return $a1;
}
这篇关于合并2个多维数组并求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!