具有相同键和值的数组的总和

时间:2019-09-01 16:17:21

标签: php laravel

我正在尝试将具有相同员工编号的数组的值求和。

这是我要求和的数组的示例。

0 => array:10 [▼
    "employee_no" => "04052018"
    "employee_id" => 317
    "company_id" => 4
    "name" => ""
    "monthly_salary" => 14000.0
    "daily_salary" => 537.0
    "work_hours" => 8
    "sss_deduction" => 0
    "pagibig_deduction" => 100
    "philhealth_deduction" => 192

0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 8
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192

它们是重复数组,我只想在其中获得工作时间总和才能得到结果

0 => array:10 [▼
"employee_no" => "04052018"
"employee_id" => 317
"company_id" => 4
"name" => ""
"monthly_salary" => 14000.0
"daily_salary" => 537.0
"work_hours" => 16
"sss_deduction" => 0
"pagibig_deduction" => 100
"philhealth_deduction" => 192

如果我要合并为我的结果集中的这个数组,则大约有24次以上发生

试过这个但没有运气

foreach ($employee_attendance as $row){

    if($row['employee_id'] === $row['employee_id']){
        $payroll_data['work_hours'] += $row['total_hours'];
    }
    $payroll[] = $payroll_data;
}

2 个答案:

答案 0 :(得分:0)

最好是从雄辩的查询本身中整理出来。但是,如果您愿意,还可以使用Collections的方法,例如groupByeach来根据员工ID过滤值,并对它们执行sum。最后,有一个结果变量来存储所有结果。

代码段:

$result = [];

$arr = $arr->groupBy('employee_no')->each(function($item,$key) use (&$result){
                $sum = $item->sum('work_hours');
                $emp_data = $item->first();
                $emp_data['work_hours'] = $sum;
                $result[] = $emp_data;
        });

完整代码:

<?php

$arr = collect([
        [
            "employee_no" => "04052018",
            "employee_id" => 317,
            "company_id" => 4,
            "name" => "",
            "monthly_salary" => 14000.0,
            "daily_salary" => 537.0,
            "work_hours" => 8,
            "sss_deduction" => 0,
            "pagibig_deduction" => 100,
            "philhealth_deduction" => 192
        ],
        [
            "employee_no" => "04052018",
            "employee_id" => 317,
            "company_id" => 4,
            "name" => "",
            "monthly_salary" => 14000.0,
            "daily_salary" => 537.0,
            "work_hours" => 8,
            "sss_deduction" => 0,
            "pagibig_deduction" => 100,
            "philhealth_deduction" => 192
        ],
        [
            "employee_no" => "04052019",
            "employee_id" => 317,
            "company_id" => 4,
            "name" => "",
            "monthly_salary" => 14000.0,
            "daily_salary" => 537.0,
            "work_hours" => 80,
            "sss_deduction" => 0,
            "pagibig_deduction" => 100,
            "philhealth_deduction" => 192
        ]
    ]);

    $result = [];

    $arr = $arr->groupBy('employee_no')->each(function($item,$key) use (&$result){
                $sum = $item->sum('work_hours');
                $emp_data = $item->first();
                $emp_data['work_hours'] = $sum;
                $result[] = $emp_data;
           });

    print_r($result);

答案 1 :(得分:0)

正如其他人所说,最好是在从数据库中检索并汇总这些工作时间后再进行计算。

但是,如果您仍然想按照上面的解释进行操作,虽然可能不是最好的方法,但是可以这样做,

foreach ($employee_attendance as $row){
    // Check if this is the first occurence of the employee number or not
    if (!isset ($payroll_data[$row['employee_no']]['work_hours']) ) {
        // First occurence will set the total hours to the current loop work hours
        $payroll_data[$row['employee_no']]['work_hours'] = $row['work_hours'];
    } else {
        // Second occurence and so on will sum previous total work hours
        $payroll_data[$row['employee_no']]['work_hours'] += $row['work_hours'];
    }
}

$payroll_data结构如下:

[
    "04052018" => [
        "work_hours" => 16 // Total work hours of this employee number
    ],
    // Repeat for other employee number
]

因此,您也更容易查看员工编号及其总工作时间。