Skip to content
Advertisement

PHP Array Group by same values and SUM

I’m trying to group by array with same value and key.

I have this array input in which i group it by employee no using the function below

  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
  ]

I already group by the array by using this function

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;
}

I used it like this one

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

proving me this kind of result

"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
    ]
  ]

What it did is to group by the array with the same value of the key employee no.

What I want now is to get the sum of total hours for every employee no. The array data contains multiple employee number. Like this one

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
    ]

I tried using array_sum but doesn’t get the result I want to.

Advertisement

Answer

Change your function to

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;
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement