扩展代码:对多维PHP数组进行分组并为每个数组元素计算特定键的总和 [英] Extended code: Grouping multidimensional PHP array and calculating sum of a particular keys for each array element

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

问题描述

这实际上是@RomanPerekhrest在此处发布的原始代码的扩展版本:

This is actually a extended version of the original code posted by @RomanPerekhrest here: Grouping multidimensional PHP array and calculating sum of a particular key for each arrays element

我玩过代码,并试图将其扩展到下一个级别.在某种程度上,我成功了.

I played with the code and tried to extend it to next level. And to some extent I succeeded.

所做的是,除了现有的"companies"子数组之外,我还添加了一个名为"holders"的子数组.然后计算"companies"子数组中"capacity_share"和"holders"子数组中"holder_share"的总和.

What is did is that I added one more sub array called "holders" in addition to the existing "companies" sub array. And then calculated the sum of "capacity_share" from "companies" sub array and "holder_share" from "holders" sub array.

它就像一种魅力.我在下面添加了扩展代码:

And it worked like a charm. I am adding the extended code below:

考虑以下分配给变量的数组:$ projects_group_by_year

Consider the below array assigned to variable: $projects_group_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] => C1                                   
                                [capacity_share] => 14                                    
                                [project_year] => 2016
                            )

                    ),
                [holders] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [project_id] => 1                                    
                                [holder_type] => H1                           
                                [holder_share] => 12                                    
                                [project_year] => 2016
                            )

                        [1] => Array
                            (
                                [id] => 2
                                [project_id] => 1                                    
                                [holder_type] => H2                                 
                                [holder_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
                            )

                    ),
                    [holders] => Array
                    (
                        [0] => Array
                            (
                                [id] => 3
                                [project_id] => 2                                 
                                [holder_type] => H1                           
                                [holder_share] => 12                                    
                                [project_year] => 2016
                            )

                        [1] => Array
                            (
                                [id] => 4
                                [project_id] => 2                                   
                                [holder_type] => H2                                 
                                [holder_share] => 14                                    
                                [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
                            )

                    ),
                    [holders] => Array
                    (
                        [0] => Array
                            (
                                [id] => 5
                                [project_id] => 3                                
                                [holder_type] => H1                           
                                [holder_share] => 12                                    
                                [project_year] => 2014
                            )

                        [1] => Array
                            (
                                [id] => 6
                                [project_id] => 3                                   
                                [holder_type] => H2                                 
                                [holder_share] => 14                                    
                                [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
                            )

                    ),
                    [holders] => Array
                    (
                        [0] => Array
                            (
                                [id] => 7
                                [project_id] => 4                                
                                [holder_type] => H1                           
                                [holder_share] => 12                                    
                                [project_year] => 2014
                            )

                        [1] => Array
                            (
                                [id] => 8
                                [project_id] => 4                                   
                                [holder_type] => H2                                 
                                [holder_share] => 14                                    
                                [project_year] => 2014
                            )

                    )

            )

    )

) 

使用下面的代码,我得到了完美的结果:

And using code below I get the perfect result:

$sumData = array_map('prepare_data', $projects_group_by_year);
print_r($sumData);
exit;

function prepare_data($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;
$arr['H2_capacity_sum'] = $arr['H1_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'];

    $h_shares = array_column($item['holders'], 'holder_share', 'holder_type');
    $arr['H1_share_sum'] += $h_shares['H1'];
    $arr['H2_share_sum'] += $h_shares['H2'];
}

return $arr;
}

但是我的代码在以下两种情况下无法正常工作:

But my code is not working well under two conditions:

第一个条件:

如果我添加所有具有相同[company_type] =>值对的子数组.然后显示以下通知:

If I add all sub arrays with same [company_type] => value pair. Then it displays following notice:

注意:未定义索引:C2

[companies] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [project_id] => 1                                    
                                [company_type] => C1  //Notice here                        
                                [capacity_share] => 12                                    
                                [project_year] => 2016
                            )

                        [1] => Array
                            (
                                [id] => 2
                                [project_id] => 1                                    
                                [company_type] => C1  //Notice here                                 
                                [capacity_share] => 14                                    
                                [project_year] => 2016
                            )

                    )

第二个条件:

如果我添加多个具有相同[company_type] =>值对的子数组,则它仅考虑最后一个[company_type] =>值对来添加[capacity_share].例如,在以下情况下,它使用[capacity_share] => 15,而不考虑[capacity_share] => 14.

If I add more than one sub array with same [company_type] => value pair, then it only considers the last [company_type] => value pair for adding the [capacity_share]. For example, it uses [capacity_share] => 15 in the case below and does not consider [capacity_share] => 14.

[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  //Notice here                                  
                                [capacity_share] => 14                                    
                                [project_year] => 2016
                            ),
                        [2] => Array
                            (
                                [id] => 3
                                [project_id] => 1                                    
                                [company_type] => C2  //Notice here                                
                                [capacity_share] => 15                                   
                                [project_year] => 2016
                            )

                    )

过去几天,我正在努力解决上述问题.

I am working on fixing above issues for past few days.

以下是我试图完成此工作的代码段之一:

Below is one of the code snippets I tried to make this work:

function prepare_data($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;
$arr['H2_share_sum'] = $arr['H1_share_sum'] = 0;

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


    $h_shares = array_column($item['holders'], 'holder_share', 'holder_type');
    $arr['H1_share_sum'] += $s_capacities['H1'];
    $arr['H2_share_sum'] += $s_capacities['H2'];
}

return $arr;
}

有人知道我在做什么错吗,我该如何解决以上两个问题?

Does any one know what I am doing wrong and how can I fix the above two issues?

谢谢.

推荐答案

这是解决方法:)

$sumData = array_map('prepare_data', $projects_grouped_by_year);
function prepare_data($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;
    $arr['H2_capacity_sum'] = $arr['H1_capacity_sum'] = 0;

    foreach ($v as $item) {
        // summing up companies shares
        $c_types = array_column($item['companies'], 'company_type');
        $c_shares = array_column($item['companies'], 'capacity_share');
        foreach ($c_types as $k => $v) {
            $arr[$v ."_capacity_sum"] += $c_shares[$k];
        }
        // summing up holders shares
        $h_types = array_column($item['holders'], 'holder_type');
        $h_shares = array_column($item['holders'], 'holder_share');
        foreach ($h_types as $k => $v) {
            $arr[$v ."_capacity_sum"] += $h_shares[$k];
        }        
    }

    return $arr;
}

print_r($sumData);

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

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