本文介绍了如何使用多个列值对子数组数据进行分组并分别对剩余的列值求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按来源和日期分组,然后按总浏览量"分组并在下面将此数组收入

How can I group by Source and date then SUM Pageviews and revenue this array below

Array
(
    [0] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-31
            [Source] => Google
            [visits] => 6000
            [Pageviews] => 12,214
            [Revenue] => 25
        )
    [1] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-31
            [Source] => Google
            [visits] => 600
            [Pageviews] => 1015
            [Revenue] => 10
        )
    [2] => Array
          (
            [Source] => Analytics
            [Date] => 2017-10-31
            [Source] => Facebook
            [visits] => 600
            [Pageviews] => 1144
            [Revenue] => 40
        )

    [3] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-30
            [Source] => Google
            [visits] => 600
            [Pageviews] => 1144
            [Revenue] => 10
        )

    [4] => Array
         (
            [Source] => Analytics
            [Date] => 2017-10-30
            [Source] => Facebook
            [visits] => 1600
            [Pageviews] => 11,445
            [Revenue] => 5
        )
     [5] => Array
         (
            [Source] => Analytics
            [Date] => 2017-10-30
            [Source] => Facebook
            [visits] => 700
            [Pageviews] => 7,445
            [Revenue] => 8
        )
)

预期结果

Array
(
    [0] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-31
            [Source] => Google
            [visits] => 6600
            [Pageviews] => 13,229
            [Revenue] => 35
        )
    [1] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-31
            [Source] => Facebook
            [visits] => 600
            [Pageviews] => 1144
            [Revenue] => 40
        )
    [2] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-30
            [Source] => Google
            [visits] => 600
            [Pageviews] => 1144
            [Revenue] => 10
        )
    [3] => Array
        (
            [Source] => Analytics
            [Date] => 2017-10-30
            [Source] => Facebook
            [visits] => 2,300
            [Pageviews] => 18,890
            [Revenue] => 35
        )
)

推荐答案

这是您需要的功能.干杯...

Here is the function you need. Cheers...

function combineAndSumUp ($myArray = []) {

    $finalArray = Array ();

    foreach ($myArray as $nkey => $nvalue) {

        $has = false;
        $fk = false;

        // Remove comma from numbers
        $nvalue['Pageviews'] = str_replace(",","",$nvalue["Pageviews"]);

        foreach ($finalArray as $fkey => $fvalue) {
            if ( ($fvalue['Date'] == $nvalue['Date']) && ($fvalue['Source'] == $nvalue['Source']) ) {
                $has = true;
                $fk = $fkey;
                break;
            }
        }

        if ( $has === false ) {
            $finalArray[] = $nvalue;
        } else {
            $finalArray[$fk]['visits'] += $nvalue['visits'];
            $finalArray[$fk]['Pageviews'] += $nvalue['Pageviews'];
            $finalArray[$fk]['Revenue'] += $nvalue['Revenue'];
        }

    }

    return $finalArray;
}

这篇关于如何使用多个列值对子数组数据进行分组并分别对剩余的列值求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 07:47