如何在PHP中合并数组

时间:2018-11-02 04:27:55

标签: php arrays

我想将相同的数组ID合并为1个数组,并使用PHP对综合浏览量进行汇总。请帮助我。

代码

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
        )
)

结果:

Array
(
    [0] => Array
        (
            [id] => 17088
            [PageView] => 9913
        )
    [1] => Array
        (
            [id] => 1121
            [PageView] => 3932
        )
    [2] => Array
        (
            [id] => 11397
            [PageView] => 5002
        )
    [3] => Array
        (
            [id] => 9638
            [PageView] => 2541
        )
)

谢谢

4 个答案:

答案 0 :(得分:1)

这很简单:

error_reporting(E_ALL ^ E_NOTICE);

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

print_r($grouped);

答案 1 :(得分: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;
}

答案 2 :(得分: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

答案 3 :(得分: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
        )

)