symfony 多维数组php -对具有相同groupRange的值求和

lvjbypge  于 2023-02-13  发布在  PHP
关注(0)|答案(2)|浏览(130)

我将试着用一些小例子来解释我的问题:我有一个表示数据库中数据的多维数组,假设输入如下所示:

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与值,任何帮助的代码?

cgvd09ve

cgvd09ve1#

盐水溶液很好,但我为乞丐提供了一个简单的解决方案。
另一个简单的解决方案:

$input = [
    [
        'groupRange' => '20-25',
        'value' => 12,
        'followersFemaleRate' => 12,
        'followersMaleRate' => 14,
    ],
    [
        'groupRange' => '30-44',
        'value' => 88,
        'followersFemaleRate' => 17,
        'followersMaleRate' => 3,
    ],
    [
        'groupRange' => '30-44',
        'value' => 32,
        'followersFemaleRate' => 17,
        'followersMaleRate' => 3,
    ],
];

$groupRangeHolder = [];
$output = [];

foreach($input as $item) {

    if( ! array_key_exists( $item['groupRange'] , $groupRangeHolder ) )
    {
        $groupRangeHolder[$item['groupRange']]['value'] = $item['value'];
        $groupRangeHolder[$item['groupRange']]['followersFemaleRate'] = $item['followersFemaleRate'];
        $groupRangeHolder[$item['groupRange']]['followersMaleRate'] = $item['followersMaleRate'];
    }
    else
    {
        $groupRangeHolder[$item['groupRange']]['value'] += $item['value'];
        $groupRangeHolder[$item['groupRange']]['followersFemaleRate'] += $item['followersFemaleRate'];
        $groupRangeHolder[$item['groupRange']]['followersMaleRate'] += $item['followersMaleRate'];
    }

}

$cur = 0;
foreach($groupRangeHolder as $key => $values) 
{
    $output[$cur]['groupRange'] = $key;
    $output[$cur]['value'] = $values['value'];
    $output[$cur]['followersFemaleRate'] = $values['followersFemaleRate'];
    $output[$cur++]['followersMaleRate'] = $values['followersMaleRate'];
}

echo "<pre>";
print_r($output);
echo "</pre>";
ia2d9nvy

ia2d9nvy2#

尝试:

$input = [
    [
        'groupRange' => '20-25',
        'value' => 12,
        'followersFemaleRate' => 12,
        'followersMaleRate' => 14,
    ],
    [
        'groupRange' => '30-44',
        'value' => 88,
        'followersFemaleRate' => 17,
        'followersMaleRate' => 3,
    ],
    [
        'groupRange' => '30-44',
        'value' => 32,
        'followersFemaleRate' => 17,
        'followersMaleRate' => 3,
    ],
];

$groupedArray = [];             
foreach( $input as $item ){
    $groupedArray[$item['groupRange']]['groupRange'] = $item['groupRange'];
    $groupedArray[$item['groupRange']]['value'] = ($groupedArray[$item['groupRange']]['value'] ?? 0) + $item['value'];
    $groupedArray[$item['groupRange']]['followersFemaleRate'] = $item['followersFemaleRate'];
    $groupedArray[$item['groupRange']]['followersMaleRate'] = $item['followersMaleRate'];
}

$output = array_values($groupedArray);

print_r($output);

输出:

Array
(
    [0] => Array
        (
            [groupRange] => 20-25
            [value] => 12
            [followersFemaleRate] => 12
            [followersMaleRate] => 14
        )

    [1] => Array
        (
            [groupRange] => 30-44
            [value] => 120
            [followersFemaleRate] => 17
            [followersMaleRate] => 3
        )

)

相关问题