我将试着用一些小例子来解释我的问题:我有一个表示数据库中数据的多维数组,假设输入如下所示:
Array
(
[0] => Array
(
[groupRange] => 20-25
[value] => 12
[followersFemaleRate] => 12
[followersMaleRate] => 14
)
[1] => Array
(
[groupRange] => 30-44
[value] => 32
[followersFemaleRate] => 17
[followersMaleRate] => 3
)
[2] => Array
(
[groupRange] => 30-44
[value] => 88
[followersFemaleRate] => 17
[followersMaleRate] => 3
)
)
我想要什么?要对值求和,followersFemaleRate,followersMaleRate具有相同的groupRange,所以输出应该是这样的:
Array
(
[0] => Array
(
[groupRange] => 20-25
[value] => 12
[followersFemaleRate] => 12
[followersMaleRate] => 14
)
[1] => Array
(
[groupRange] => 30-44
[value] => 120
[followersFemaleRate] => 34
[followersMaleRate] => 6
)
)
我的代码:
$RangeArray = [];
foreach($dbProfile->getData() as $d) {
foreach ($d->getGroupPercentages() as $x){
$ageRangeSingleArray['groupRange'] = $x->getGroupRange();
$ageRangeSingleArray['value'] = $x->getValue();
$ageRangeSingleArray['followersFemaleRate'] = $x->getFollowerGenderFemale();
$ageRangeSingleArray['followersMaleRate'] = $x->getFollowerGenderMale();
$RangeArray [] = $ageRangeSingleArray;
}
}
但是我卡住了,我的想法是首先检查groupRage是否已经存在,如果是,求和该范围的值,如果没有添加新的元素groupRange与值,任何帮助的代码?
2条答案
按热度按时间cgvd09ve1#
盐水溶液很好,但我为乞丐提供了一个简单的解决方案。
另一个简单的解决方案:
ia2d9nvy2#
尝试:
输出: