将 2d 数组数据按 2 列分组,形成 3d 关联数组,每组中包含总列值

问题描述 投票:0回答:2

我有一个包含以下数据的二维数组:

[
    [
        "totalTonne" => 11180,
        "wasteHierarchy" => "Disposal (Landfill)",
        "completionDate" => "2014-05-14"
    ],
    [
        "totalTonne" => 820,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-06-14"
    ],
    [
        "totalTonne" => 21040,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-08-14"
    ],
    [
        "totalTonne" => 7020,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-11-14"
    ],
    [
        "totalTonne" => 6660,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-07-14"
    ],
    [
        "totalTonne" => 12000,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-09-14"
    ],
    [
        "totalTonne" => 8900,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-10-14"
    ],
    [
        "totalTonne" => 9100,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-12-14"
    ],
    [
        "totalTonne" => 5520,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2015-01-15"
    ],
    [
        "totalTonne" => 1900,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-08-14"
    ],
    [
        "totalTonne" => 5950,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-11-14"
    ],
    [
        "totalTonne" => 12000,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-07-14"
    ],
    [
        "totalTonne" => 10480,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-09-14"
    ],
    [
        "totalTonne" => 8900,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-10-14"
    ],
    [
        "totalTonne" => 10060,
        "wasteHierarchy" => "Recycling",
        "completionDate" => "2014-12-14"
    ],
    [
        "totalTonne" => 15500,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-06-14"
    ],
    [
        "totalTonne" => 8850,
        "wasteHierarchy" => "Energy Recovery",
        "completionDate" => "2014-11-14"
    ]
]

我想将其转换为 3d 数组,该数组按第一级上的

wasteHierarchy
值分组,然后按第二级上的
completionDate
值分组,然后对每组中的
totalTonne
值进行求和。

所需输出:

[Disposal (Landfill)] => Array
    (
        [May 14] => 11180
    )

[Energy Recovery] => Array
    (
        [Jun 14] => 16320
        [Aug 14] => 22940
        [Nov 14] => 21820
    )

[Recycling] => Array
    (
        [Jul 14] => 18660
        [Sep 14] => 22480
        [Oct 14] => 17800
        [Dec 14] => 19160
        [Jan 15] => 5520
    )

到目前为止,我拥有的 PHP 代码如下:

$whmOutput = Array();
foreach($wasteHierMon as $whm) {
    $dateChanged = date("M y", strtotime($whm['completionDate']));

    $whmOutput_element = &$whmOutput[$dateChanged];
    $whmOutput_element['wasteHierarchy'] = $whm['wasteHierarchy'];
    $whmOutput_element['completionDate'] = $dateChanged;
    !isset($whmOutput_element['totalUom']) && $whmOutput_element['totalUom'] = 0;
    $whmOutput_element['totalUom'] += $whm['totalTonne'];

}
$newWHM = array();          
foreach($whmOutput as $whm) {
    $dateChanged = $whm['completionDate'];

    $wasteHierarchy = $whm['wasteHierarchy'];
    $totalUom = +$whm['totalUom'];

    $newWHM[$wasteHierarchy][$dateChanged] = $totalUom;
}

但是,该代码并不能正确完成这项工作。虽然它创建了我想要的结构,但它没有正确地重新排列数据。它无法正确对日期进行分组,因此,

totalTonne
值不正确。

如何修改代码才能实现此目的?

php arrays multidimensional-array sum grouping
2个回答
1
投票

类似这样的东西应该可以工作:演示

$whmOutput = array();
foreach ($wasteHierMon as $whm) {
    $dateChanged = date("M y", strtotime($whm['completionDate']));
     // if not defined, define with value zero
    if (!isset($whmOutput[$whm['wasteHierarchy']][$dateChanged])) {
        $whmOutput[$whm['wasteHierarchy']][$dateChanged] = 0;
    }
    // addition will always work now
    $whmOutput[$whm['wasteHierarchy']][$dateChanged] += $whm['totalTonne'];
}

0
投票

对于那些喜欢从函数迭代返回结果的人来说,

array_reduce()
是理想的选择,因为不能保证结果数组与输入数组具有相同的计数。

代码:(演示

var_export(
    array_reduce(
        $array,
        function($result, $row) {
            $group = date("M y", strtotime($row['completionDate']));
            $result[$row['wasteHierarchy']][$group] = ($result[$row['wasteHierarchy']][$group] ?? 0) + $row['totalTonne'];
            return $result;
        }
    )
);

为了展示 @charlietfl 答案的替代方案,我将演示带有数组解构语法的

foreach()
,以在循环体中享受方便的单个变量。 (演示)

$result = [];
foreach ($array as ['totalTonne' => $total, 'wasteHierarchy' => $cat, 'completionDate' => $date]) {
    $group = date("M y", strtotime($date));
    $result[$cat][$group] = ($result[$cat][$group] ?? 0) + $total;
}
var_export($result);
© www.soinside.com 2019 - 2024. All rights reserved.