PHP数组按相同值和SUM分组

时间:2019-09-09 10:46:33

标签: php arrays laravel

我正在尝试按具有相同值和键的数组分组。

我有这个数组输入,其中没有使用下面的函数按员工对它进行分组

  0 => array:7 [▼
    "employee_no" => "04052018"
    "employee_id" => 317
    "company_id" => 4
    "name" => ""
    "total_hours" => 8
    "monthly_salary" => 15898.53
    "daily_salary" => 611.48
  ]
  1 => array:7 [▼
    "employee_no" => "06282018"
    "employee_id" => 319
    "company_id" => 4
    "name" => ""
    "total_hours" => 8
    "monthly_salary" => 32553.0
    "daily_salary" => 1252.04
  ]
  2 => array:7 [▼
    "employee_no" => "09291998"
    "employee_id" => 320
    "company_id" => 4
    "name" => " "
    "total_hours" => 8
    "monthly_salary" => 108916.75
    "daily_salary" => 4189.11
  ]
  3 => array:7 [▼
    "employee_no" => "12052011"
    "employee_id" => 323
    "company_id" => 4
    "name" => ""
    "total_hours" => 8
    "monthly_salary" => 36601.03
    "daily_salary" => 1407.73
  ]

我已经通过使用此功能按数组分组

private function group_by($key, $data) {
    $result = array();

    foreach($data as $val) {
        if(array_key_exists($key, $val)){
            $result[$val[$key]][] = $val;
        }else{
            $result[""][] = $val;
        }
    }

    return $result;
}

我像这样使用它

$data = $this->group_by("employee_no", $employee_attendance);

证明我这种结果

"04052018" => array:7 [▼
    0 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 317
      "company_id" => 4
      "name" => ""
      "total_hours" => 8
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
    1 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 328
      "company_id" => 4
      "name" => ""
      "total_hours" => 8
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
    2 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 343
      "company_id" => 4
      "name" => ""
      "total_hours" => 8
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
    3 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 370
      "company_id" => 4
      "name" => ""
      "total_hours" => 2
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
    4 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 377
      "company_id" => 4
      "name" => ""
      "total_hours" => 2
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
    5 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 385
      "company_id" => 4
      "name" => ""
      "total_hours" => 8
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
    6 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 396
      "company_id" => 4
      "name" => ""
      "total_hours" => 8
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]
  ]

它所做的是按照与关键员工编号相同的值对数组进行分组。

我现在想要的是获取每位员工的总时数。数组数据包含多个员工编号。像这样

0 => array:7 [▼
      "employee_no" => "04052018"
      "employee_id" => 317
      "company_id" => 4
      "name" => ""
      "total_hours" => 48 -> calculated total hours
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]

1 => array:7 [▼
      "employee_no" => "04052019"
      "employee_id" => 318
      "company_id" => 4
      "name" => ""
      "total_hours" => 24 -> calculated total hours
      "monthly_salary" => 15898.53
      "daily_salary" => 611.48
    ]

我尝试使用 array_sum ,但没有得到我想要的结果。

1 个答案:

答案 0 :(得分:3)

将功能更改为

private function group_by($key, $data) {
    $result = array();
    foreach($data as $val) {
        array_key_exists($val[$key], $result) 
        ?
        ($result[$val[$key]]['total_hours'] += $val['total_hours'])
        :
        ($result[$val[$key]] = $val);
    }
    return $result;
}