对多维PHP数组进行分组并为每个数组元素计算特定键的总和 [英] Grouping multidimensional PHP array and calculating sum of a particular key for each arrays element

查看:51
本文介绍了对多维PHP数组进行分组并为每个数组元素计算特定键的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中包含有关所有项目的数据.

I had an array which had data about all projects.

我需要按'year_actual'元素对数组进行分组.

I needed to group the array by 'year_actual' element.

我已成功使用php函数"array_group_by"完成了此分组,该函数可在此处使用: https://gist.github.com/mcaskill/baaee44487653e1afc0d

I have successfully done this grouping using a php function "array_group_by" which is available here: https://gist.github.com/mcaskill/baaee44487653e1afc0d

这是我分配给变量的分组数组:$ projects_grouped_by_year

Here is my grouped array which I assigned to a variable: $projects_grouped_by_year

Array
(
    [2016] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [project_name] => P1                    
                    [project_capacity] => 100                    
                    [year_actual] => 2016                    
                    [companies] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1
                                    [project_id] => 1                                    
                                    [company_type] => C1                           
                                    [capacity_share] => 12                                    
                                    [project_year] => 2016
                                )

                            [1] => Array
                                (
                                    [id] => 2
                                    [project_id] => 1                                    
                                    [company_type] => C2                                    
                                    [capacity_share] => 14                                    
                                    [project_year] => 2016
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [project_name] => P2                    
                    [project_capacity] => 200                    
                    [year_actual] => 2016                    
                    [companies] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 3
                                    [project_id] => 2                                    
                                    [company_type] => C2                                    
                                    [capacity_share] => 15                                    
                                    [project_year] => 2016
                                )

                            [1] => Array
                                (
                                    [id] => 4
                                    [project_id] => 2                                    
                                    [company_type] => C1                                    
                                    [capacity_share] => 16                                    
                                    [project_year] => 2016
                                )

                        )

                )

        )

    [2014] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [project_name] => P3                    
                    [project_capacity] => 300                    
                    [year_actual] => 2014                    
                    [companies] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 5
                                    [project_id] => 3                                    
                                    [company_type] => C1                                    
                                    [capacity_share] => 20                                    
                                    [project_year] => 2014
                                )

                            [1] => Array
                                (
                                    [id] => 6
                                    [project_id] => 3                                    
                                    [company_type] => C2                                  
                                    [capacity_share] => 22                                    
                                    [project_year] => 2014
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 4
                    [project_name] => P4                    
                    [project_capacity] => 400                    
                    [year_actual] => 2014                    
                    [companies] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 7
                                    [project_id] => 4                                    
                                    [company_type] => C2                                    
                                    [capacity_share] => 11                                    
                                    [project_year] => 2014
                                )

                            [1] => Array
                                (
                                    [id] => 8
                                    [project_id] => 4
                                    [company_type] => C1
                                    [capacity_share] => 10
                                    [project_year] => 2014
                                )

                        )

                )

        )

) 

我正在操纵上述数组以创建新的以下结果数组:我将创建一个新数组,并将计算出的值插入此新数组中:

I am manipulating above array to create a new following resultant array: I will create a new array and insert the calculated values in this new array:

Array
(
    [0] => Array(
        //year_actual of project
        'year' => 2016,
         //[100+200] : sum of 'project_capacity' where year_actual = 2016 
        'project_capacity_sum' => 300,
         //[12+16] : sum of 'capacity_share' where company_type = C1 and project_year = 2016
        'C1_capacity_sum' => 28,
         //[14+15] : sum of 'capacity_share' where company_type = C2 and project_year = 2016
        'C2_capacity_sum' => 29
        )
    [1] => Array(
         //year_actual of project
        'year' => 2014,
         //[300+400] : sum of 'project_capacity' where year_actual = 2014
        'project_capacity_sum' => 700,
         //[20+10] : sum of 'capacity_share' where company_type = C1 and project_year = 2014
        'C1_capacity_sum' => 30,
         //[22+11] : sum of 'capacity_share' where company_type = C2 and project_year = 2014
        'C2_capacity_sum' => 33
        )
); 

我成功使用以下代码计算了"project_capacity_sum"字段:

I have succeeded in calculating the field "project_capacity_sum" using the below code:

$ projectCapacitySum = array_map(function($ data){返回array_sum(array_column($ data,'project_capacity'));},$ projects_grouped_by_year);

但是我正在努力计算最近两天的其他字段.

But I am struggling to calculate other fields for the last two days.

对我来说唯一有用的列如下:

The only useful columns to me are following:

[project_capacity]和[year_actual]

[project_capacity] and [year_actual]

在"companies"子数组内部

Inside "companies" sub array

[company_type]和[capacity_share]

[company_type] and [capacity_share]

基本上,我正在尝试按"year_actual"对数组进行分组(我已经完成了)然后找到每年的总"project_capacity",以及每年的每个"company_type"的总"capacity_share".我希望这是有道理的.

Basically I am trying group the array by "year_actual" (which i have already done) Then find the total "project_capacity" in each year and also total "capacity_share" for each "company_type" in each year. I hope this makes sense.

如果我们使用[companies]子数组中的[project_year]列来实现所需的输出,就没有问题.

There is no problem if we use column [project_year] inside the [companies] sub array to achieve the desired output.

我知道我必须在companies子数组中进行另一个小组的工作,而我仍在尝试这样做.

I know that I have to do another group by inside the companies sub array..and I am still trying to do so.

我现在不关心代码效率或优化.我只是在寻找一种可行的逻辑.

I am not concerned about code efficiency or optimization at this point. I am just looking for a working logic.

我正在寻求帮助.请帮助我.

I am asking for help. Please help me with this.

谢谢.

推荐答案

要获得所需的结果,请扩展您的初始解决方案( array_map + array_sum + array_column ),方法如下:

To get the needed result extend your initial solution (array_map + array_sum + array_column) with the following approach:

$sumData = array_map(function ($v) {
    $arr = ['year' => current(array_column($v, 'year_actual'))];
    $arr['project_capacity_sum'] = array_sum(array_column($v, "project_capacity"));
    $arr['C2_capacity_sum'] = $arr['C1_capacity_sum'] = 0;

    foreach ($v as $item) {  // iterating through the nested items
        $c_capacities = array_column($item['companies'], 'capacity_share', 'company_type');
        $arr['C1_capacity_sum'] += $c_capacities['C1'];
        $arr['C2_capacity_sum'] += $c_capacities['C2'];
    }

    return $arr;
}, $projects_grouped_by_year);

print_r($sumData);

输出:

Array
(
    [2016] => Array
        (
            [year] => 2016
            [project_capacity_sum] => 300
            [C1_capacity_sum] => 28
            [C2_capacity_sum] => 29
        )

    [2014] => Array
        (
            [year] => 2014
            [project_capacity_sum] => 700
            [C1_capacity_sum] => 30
            [C2_capacity_sum] => 33
        )
)

这篇关于对多维PHP数组进行分组并为每个数组元素计算特定键的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆