将二维数组行数据按两列分组,并为每个用户保留运行总计

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

我有一个数组输出如下。

 array:7 [
      0 => array:5 [
        "count" => "6"
        "year" => "2019"
        "month" => "10"
        "name" => "P"
        "id" => 3
      ]
      1 => array:5 [
        "count" => "2"
        "year" => "2019"
        "month" => "10"
        "name" => "s.vimal"
        "id" => 1
      ]
      2 => array:5 [
        "count" => "4"
        "year" => "2019"
        "month" => "10"
        "name" => "MR"
        "id" => 2
      ]
      3 => array:5 [
        "count" => "4"
        "year" => "2019"
        "month" => "11"
        "name" => "vimaltest"
        "id" => 10
      ]
      4 => array:5 [
        "count" => "11"
        "year" => "2019"
        "month" => "11"
        "name" => "s.vimal"
        "id" => 1
      ]
      5 => array:5 [
        "count" => "1"
        "year" => "2019"
        "month" => "12"
        "name" => "MR"
        "id" => 2
      ]
      6 => array:5 [
        "count" => "1"
        "year" => "2019"
        "month" => "12"
        "name" => "vimaltest"
        "id" => 10
      ]
    ]

但是我需要创建一个数组来总结相似名称的数量,并每月逐渐增加。

预期输出。

array:7 [
  0 => array:5 [
    "P"=>"6",
    "s.vimal"=>"2",
    "MR"=>"4"
    "year" => "2019"
    "month" => "10"
  ]
  1 => array:6 [
    "P"=>"6",
    "s.vimal"=>"13",
    "vimaltest"=>"4",
    "MR"=>"4"
    "year" => "2019"
    "month" => "11"

  ]
  2 => array:6 [
    "P"=>"6",
    "s.vimal"=>"13",
    "vimaltest"=>"5",
    "MR"=>"5"
    "year" => "2019"
    "month" => "12"
  ]
]

预期输出是基于每个月的输入的汇总。当你查看预期输出时你就会得到它。

php arrays multidimensional-array sum grouping
1个回答
1
投票

首先,您可以收集独特的值,例如

year
months

$mon_ar = array_unique(array_column($ar,'month'));
$year_ar = array_unique(array_column($ar,'year')); 

然后您可以使用下一个

foreach
循环循环数据数组:

foreach($ar as $rec){                       // each record
    foreach($year_ar as $year){             // each year
        foreach($mon_ar as $month){         // each month

            // if this is a new 'year.month' (201910,201911...)
            if (!isset($result[$year.$month])) 
            $result[$year.$month] = [ 'month' => $month, 'year' => $year];        

            // if month equal to current or it's previous month
            if ($rec['year'] == $year && $rec['month'] <= $month) { 
                // if this is a new 'name' value
                if (!isset($result[$year.$month][$rec['name']])) 
                $result[$year.$month][$rec['name']] = 0;
                // summing count to the existing 'name' value 
                $result[$year.$month][$rec['name']] += $rec['count']; 
            }
        }
    }
}

输出将类似于:

Array
(
    [201910] => Array
        (
            [month] => 10
            [year] => 2019
            [P] => 6
            [s.vimal] => 2
            [MR] => 4
        )

    [201911] => Array
        (
            [month] => 11
            [year] => 2019
            [P] => 6
            [s.vimal] => 13
            [MR] => 4
            [vimaltest] => 4
        )

    [201912] => Array
        (
            [month] => 12
            [year] => 2019
            [P] => 6
            [s.vimal] => 13
            [MR] => 5
            [vimaltest] => 5
        )

)

现在您可以使用

sort()
将索引重写为
0,1,2,...

sort($result);

演示

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