在多维数组中对值进行分组和求和时未定义的索引

Undefined index when Grouping and sum values in multidimensional array

我正在使用以下代码按 货币 对数组 $summary 进行分组,并获得分组持续时间和成本的总和。到目前为止,我能够对数组进行分组和求和,但是由于数组单元格 $result[$split['currency']]['duration']$result[$split['currency']]['cost'] 未定义,所以当我 运行 代码时我会注意到。如何在不使用 error_reporting(0) 的情况下删除通知?

代码

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

编辑

$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会如下图

 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
    )

)

您需要先定义数组:

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

也测试 $result 个数组索引是否存在。

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

我不明白额外条件块的必要性。

使用currency作为临时键。在第一次遇到 currency 时,存储整行。否则,将当前迭代的 durationcost 添加到存储组。完成循环后,用 array_values().

重新索引数组

代码:(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));

输出:

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,
  ),
)