PHP数组:分组依据和求和值

时间:2017-11-15 01:59:10

标签: php arrays

如何按来源和日期进行分组,然后按页面浏览量分组,并将此数组收入

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

3 个答案:

答案 0 :(得分:1)

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

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;
}

答案 1 :(得分:1)

评论中@ J.D.Pace表示什么:

您打算如何区分每个数组中的两个Source元素?

是...

“您的传入数据是不可能的,因为在同一阵列的同一级别中可能没有两个相同的键。您的数据在同一子阵列中显示"Source" => "Analytics""Source" => "Facebook"。” ...这意味着您的样本输入数据不准确。

因为我们不知道您的情况 ,并且由于Analytics值对于此任务而言完全毫无价值,所以我将提供一种解决方案,该方案将忽略这些不准确的子数组元素

其他答案过于努力,编写了太多代码。既不必编写多个/嵌套循环,也不必对数据进行预排序。通过在输出数组中分配“复合临时键”并将处理过程中的输出数组用作“查找数组”,只需一个循环即可轻松完成此任务。 isset()是确定当前子数组是否包含新数据组或以前是否遇到过该组的非常快速的方法。

完成后,您可以选择调用array_values()重新索引输出(如果需要)。您可能还想重申一下,以重新设置网页浏览量的格式,以包含逗号-再次由您决定。

代码:(Demo

$array = [
    ['Date' => '2017-10-31', 'Source' => 'Google', 'visits' => '6000', 'Pageviews' => '12,214', 'Revenue' => '25'],
    ['Date' => '2017-10-31', 'Source' => 'Google', 'visits' => '600', 'Pageviews' => '1015', 'Revenue' => '10'],
    ['Date' => '2017-10-31', 'Source' => 'Facebook', 'visits' => '600', 'Pageviews' => '1144', 'Revenue' => '40'],
    ['Date' => '2017-10-30', 'Source' => 'Google', 'visits' => '600', 'Pageviews' => '1144', 'Revenue' => '10'],
    ['Date' => '2017-10-30', 'Source' => 'Facebook', 'visits' => '1600', 'Pageviews' => '11,445', 'Revenue' => '5'],
    ['Date' => '2017-10-30', 'Source' => 'Facebook', 'visits' => '700', 'Pageviews' => '7,445', 'Revenue' => '8'],
];

foreach ($array as $subarray) {
    $tempKey = $subarray['Source'].$subarray['Date'];  // this is the compound value
    $subarray['Pageviews'] = str_replace(',', '', $subarray['Pageviews']); // get rid of meddlesome comma
    if (isset($result[$tempKey])) {
        $result[$tempKey]['visits'] += $subarray['visits'];
        $result[$tempKey]['Pageviews'] += $subarray['Pageviews'];
        $result[$tempKey]['Revenue'] += $subarray['Revenue'];
    } else {
        $result[$tempKey] = $subarray;
    }
}
var_export(array_values($result));

输出:(摘要)

array (
  0 => 
  array (
    'Date' => '2017-10-31',
    'Source' => 'Google',
    'visits' => 6600,
    'Pageviews' => 13229,
    'Revenue' => 35,
  ),
  ...
)

答案 2 :(得分:0)

假设只有一个Source索引。

试试这个:

array_multisort(array_column($arr, 'Date'), SORT_DESC,array_column($arr, 'Source'), SORT_DESC, $arr);

$new_arr = [];
$temp = ["Date"=>""];
$ctr = 0;
foreach($arr as $val) {
    if($val["Date"] == $temp["Date"] && $val["Source"] == $temp["Source"]) {
        $ctr2 = $ctr - 1;
        $new_arr[$ctr2]["visits"] += $val["visits"];
        $new_arr[$ctr2]["Pageviews"] += $val["Pageviews"];
        $new_arr[$ctr2]["Revenue"] += $val["Revenue"];
    } else {
        $new_arr[$ctr++] = $val;
    }
    $temp = $val;
}

print_r($new_arr);