1

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.

AmyllaVimiar
  • 315
  • 5
  • 24
  • What is the content of your input array? – Zhorov Sep 09 '19 at 10:48
  • Try this: `array_sum(array_column($data['%id%'], 'total_hours'));` – Grabatui Sep 09 '19 at 10:50
  • Try `$total_hours = array_sum(array_column($data["04052018"], 'total_hours'))` – Nick Sep 09 '19 at 10:50
  • I updated the answer with the array collection before i group it by – AmyllaVimiar Sep 09 '19 at 10:52
  • That contains many different employee numbers - so do you want a result for only that specific ID `04052018` in the end, or for all of them? You said you wanted a “single result” … – misorude Sep 09 '19 at 10:54
  • What I mean is for a particular employee, example 04052018 I want a summarized result in which the total hours work is summed. – AmyllaVimiar Sep 09 '19 at 10:55
  • I updated my question with more actual result and data collection array – AmyllaVimiar Sep 09 '19 at 10:57
  • Your code that you have shown appears to make rather little sense. `if(array_key_exists($key, $val))` – why are you checking for a key inside $val here? Shouldn’t you be checking if $result contains an entry with that key value already? And what is `$result[""][] = $val;` for - do you have elements that might not even contain the key you are looking for? – misorude Sep 09 '19 at 10:58
  • I might have an element which doesn't contain employee no so i check it there instead to avoid errors – AmyllaVimiar Sep 09 '19 at 10:59

1 Answers1

3

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;
}
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20