按标识列对二维数组的行进行分组,并对每组内的另一列求和[重复]

问题描述 投票:0回答:4

我想将具有相同 id 的行合并为一行并对综合浏览量值进行求和。

[
    ['id' => 17088, 'PageView' => 5575],
    ['id' => 17088, 'PageView' => 4338],
    ['id' => 1121, 'PageView' => 3922],
    ['id' => 11397, 'PageView' => 2625],
    ['id' => 9638, 'PageView' => 2541],
    ['id' => 11397, 'PageView' => 2377],
    ['id' => 1121, 'PageView' => 10],
]

想要的结果:

[
    ['id' => 17088, 'PageView' => 9913],
    ['id' => 1121, 'PageView' => 3932],
    ['id' => 11397, 'PageView' => 5002],
    ['id' => 9638, 'PageView' => 2541],
]
php arrays multidimensional-array sum grouping
4个回答
1
投票

非常简单:

error_reporting(E_ALL ^ E_NOTICE);

foreach ($array as $row)
  $grouped[$row['id']] += $row['PageView'];

print_r($grouped);

0
投票

您可以使用以下逻辑,

$test = [1=> ['id'=>10, 'sum'=>10],['id'=>12, 'sum'=>10],['id'=>10, 'sum'=>10]];

    merge($test);
function merge($array) 
{
    $tempArray = array();
    foreach($array as $key=>$value) {
        if(array_key_exists($value['id'],$tempArray)) {
           $tempArray[$value['id']]['sum'] = $tempArray[$value['id']]['sum'] +  $value['sum'];
        } else {
            $tempArray[$value['id']] = $value;
        }
    }
    print "<pre>"; print_r($tempArray); exit;
}

0
投票

如果键已经存在,只需循环数组并求和即可。
我在 id 上创建了一个关联数组,这使得在循环中求和变得很容易。
如果您想要一个索引数组,您可以在循环后使用 array_values。

foreach($arr as $sub){
    if(isset($new[$sub['id']])){
        $new[$sub['id']]['PageView'] = $new[$sub['id']]['PageView'] + $sub['PageView'];
    }else{
        $new[$sub['id']] = $sub;
    }

}
var_dump($new);

输出:

array(4) {
  [17088]=>
  array(2) {
    ["id"]=>
    string(5) "17088"
    ["PageView"]=>
    int(9913)
  }
  [1121]=>
  array(2) {
    ["id"]=>
    string(4) "1121"
    ["PageView"]=>
    int(3932)
  }
  [11397]=>
  array(2) {
    ["id"]=>
    string(5) "11397"
    ["PageView"]=>
    int(5002)
  }
  [9638]=>
  array(2) {
    ["id"]=>
    string(4) "9638"
    ["PageView"]=>
    string(4) "2541"
  }
}

https://3v4l.org/W3Afa


0
投票

试试这个代码-

这是你的数组 -

$a = array(0=>array('id' => 17088,'PageView' => 5575),1=>array('id' => 17088,'PageView' => 4338),2=>array('id' => 1121,'PageView' => 3922),3=>array('id' => 11397,'PageView' => 2625),4=>array('id' => 9638,'PageView' => 2541),5=>array('id' => 11397,'PageView' => 2377),6=>array('id' => 1121,'PageView' => 10));

    $b=array();

    foreach ($a as $key => $value) {
        if(isset($b[$value['id']]) && count($b[$value['id']])){
            $b[$value['id']]['PageView']=$b[$value['id']]['PageView']+$value['PageView'];
        }else{
            $b[$value['id']]['id']=$value['id'];
            $b[$value['id']]['PageView']=$value['PageView'];
        }
    }

    print_r($b);

输出 -

Array
(
    [17088] => Array
        (
            [id] => 17088
            [PageView] => 9913
        )

    [1121] => Array
        (
            [id] => 1121
            [PageView] => 3932
        )

    [11397] => Array
        (
            [id] => 11397
            [PageView] => 5002
        )

    [9638] => Array
        (
            [id] => 9638
            [PageView] => 2541
        )

)
© www.soinside.com 2019 - 2024. All rights reserved.