1

I am using the below code to group the array $summary by currency and get the sum of grouped duration and cost. So far I am able to group and sum the array but since the array cells $result[$split['currency']]['duration'] and $result[$split['currency']]['cost'] are undefined I am getting notice when I run the code. How can I remove the notice without using error_reporting(0)?

Code

foreach ($summary as $split) {

            if (isset($split['currency'])) {
                $result[$split['currency']]['duration'] += $split['duration'];
                $result[$split['currency']]['cost'] += $split['cost'];
            } else {
                $result[0]['duration'] += $split['duration'];
                $result[0]['cost'] += $split['cost'];
            }
        }

EDIT

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

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

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

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

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

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

$result will be as show below

 Array
(
[0] => Array
    (
        [currency] => SGD
        [duration] => 24
        [cost] => 685.71
    )

[1] => Array
    (
        [currency] => MYR
        [duration] => 24
        [cost] => 685.72
    )

[2] => Array
    (
        [currency] => 
        [duration] => 8
        [cost] => 0
    )

)

5
  • 2
    show us the input array values and expected outcome. thanks Commented Jul 27, 2016 at 8:24
  • what is the possible outputs of $split['currency']? Commented Jul 27, 2016 at 8:24
  • show us your array values so that it will be easy for us to give you a solution. Commented Jul 27, 2016 at 8:25
  • var_dump($summary); and post the result. Commented Jul 27, 2016 at 8:25
  • Edit: included arrays in my question Commented Jul 27, 2016 at 8:35

3 Answers 3

1

You need to define the array first:

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

Comments

1

Test existence of $result array indexes too.

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

Comments

0

I don't understand the need for the extra condition block.

Use currency as temporary keys. On the first encounter of a currency, store the whole row. Otherwise, add the current iteration's duration and cost to the stored group. When finished looping, re-index the array with array_values().

Code: (Demo)

$result = [];
foreach ($array as $row) {
    if (!isset($result[$row['currency']])) {
        $result[$row['currency']] = $row;
    } else {
        $result[$row['currency']]['duration'] += $row['duration'];
        $result[$row['currency']]['cost'] += $row['cost'];
    }
}
var_export(array_values($result));

Output:

array (
  0 => 
  array (
    'currency' => 'SGD',
    'duration' => 16.0,
    'cost' => 457.14,
  ),
  1 => 
  array (
    'currency' => NULL,
    'duration' => 8.0,
    'cost' => NULL,
  ),
  2 => 
  array (
    'currency' => 'MYR',
    'duration' => 24.0,
    'cost' => 685.72,
  ),
)

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.