3

Code

foreach ($summary as $split) {
    if (isset($split['currency'])) {
        if (!isset($result[$split['currency']]) {
            $result[$split['currency']] = [
                'duration' => 0,
                'cost' => 0
            ];
        }
        $result[$split['currency']]['employee'] = $split['employee'];
        $result[$split['currency']]['duration'] += $split['duration'];
        $result[$split['currency']]['cost'] += $split['cost'];
    } else {
        $result[0]['employee'] = $split['employee'];
        $result[0]['duration'] += $split['duration'];
        $result[0]['cost'] += $split['cost'];
    }
}

My array looks like this

$summary = Array
(
[0] => Array
   (
    [employee] => John
    [currency] => SGD
    [duration] => 8.00
    [cost] => 228.57
)

[1] => Array
   (
    [employee] => Fil
    [currency] => SGD
    [duration] => 8.00
    [cost] => 228.57
 )

[2] => Array
(
    [employee] => John
    [currency] => 
    [duration] => 8.00
    [cost] => 
)

[3] => Array
(
    [employee] => John
    [currency] => MYR
    [duration] => 12.00
    [cost] => 342.86
)

[4] => Array
(
    [employee] => Sam
    [currency] => SGD
    [duration] => 8.00
    [cost] => 228.57
)

[5] => Array
(
    [employee] => Fil
    [currency] => MYR
    [duration] => 12.00
    [cost] => 342.86
)

I want to group the above array by employee and currency. What I require is similar to Stackoverflow

but in this grouping is done for currency only so in the output array there will be 3 inner array only.

I want to group the array such that the result will group first employee and then currency. So there will be 3 inner array for each employee

2 Answers 2

1

just add the name as the key in $result array before currency key,

foreach ($summary as $split) {
        if (isset($split['currency'])) {
            if (!isset($result[$split['employee']][$split['currency']]) {
                $result[$split['employee']][$split['currency']] = [
                    'duration' => 0,
                    'cost' => 0
                ];
            }
            $result[$split['employee']][$split['currency']]['employee'] = $split['employee'];
            $result[$split['employee']][$split['currency']]['duration'] += $split['duration'];
            $result[$split['employee']][$split['currency']]['cost'] += $split['cost'];
        } else {
            $result[0]['employee'] = $split['employee'];
            $result[0]['duration'] += $split['duration'];
            $result[0]['cost'] += $split['cost'];
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$in = [
   ['employee'=>'A','currency'=>'USD','duration'=>8.00,'cost'=>42]
  ,['employee'=>'A','currency'=>'SDU','duration'=>8.00,'cost'=>42]
  ,['employee'=>'A','currency'=>'','duration'=>8.00,'cost'=>42]
  ,['employee'=>'B','currency'=>'USD','duration'=>8.00,'cost'=>42]
  ,['employee'=>'B','currency'=>'SDU','duration'=>8.00,'cost'=>42]
  ,['employee'=>'B','currency'=>'','duration'=>8.00,'cost'=>42]
];

$result = [];
$groupSum = function ($v) use (&$result) {
    $emp = $v['employee'];
    $curr = $v['currency'];
    if ( !array_key_exists($emp,$result) ) {
      $result[$emp] = [];
    }
    if ( !array_key_exists($curr,$result[$emp]) ) {
        $result[$emp][$curr] = ['duration'=>0,'cost'=>0];
    }
    $result[$emp][$curr]['duration'] += $v['duration'];
    $result[$emp][$curr]['cost'] += $v['cost'];
};
array_map($groupSum,$in);

print_r($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.