将多维数组与标准数组进行比较并填写缺失值


Compare a multidimensional array with a standard array and fill in missing values

我有这个数组:

Array
(
    [Recycling] => Array
        (
            [May 14] => 7040
            [Jul 14] => 3920
            [Aug 14] => 14560
            [Sep 14] => 15120
            [Oct 14] => 12420
            [Nov 14] => 13440
            [Dec 14] => 13720
            [Jan 15] => 3920
        )
    [Disposal (Landfill)] => Array
        (
            [May 14] => 3800
            [Jun 14] => 7820
            [Jul 14] => 8100
            [Aug 14] => 5520
        )
    [Energy Recovery] => Array
        (
            [May 14] => 340
            [Jun 14] => 8500
            [Jul 14] => 6640
            [Aug 14] => 2860
            [Sep 14] => 7360
            [Oct 14] => 5380
            [Nov 14] => 8380
            [Dec 14] => 5440
            [Jan 15] => 1600
        )
)

这是使用以下查询生成的:

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

我遇到的问题是数组的第 2 级部分并不总是有一个值(这可以从数组的 RecyclingDisposal 部分中看到。

我有另一个使用此代码的数组:

        $minValue = min($wasteHierMon);
        $minDate = $minValue['completionDate'];
        $time1  = strtotime($minDate);
        $time2  = strtotime($de);
        $my     = date('M y', $time2);
        $months = array(date('M y', $time1));
        while($time1 < $time2) {
          $time1 = strtotime(date('Y-m-d', $time1).' +1 month');
          if(date('mY', $time1) != $my && ($time1 < $time2))
             $months[] = date('M y', $time1);
        }
        $months[] = date('M y', $time2);

这将生成以下数组:

Array
(
    [0] => May 14
    [1] => Jun 14
    [2] => Jul 14
    [3] => Aug 14
    [4] => Sep 14
    [5] => Oct 14
    [6] => Nov 14
    [7] => Dec 14
    [8] => Jan 15
)

此数组是通过在第一个数组中查找最低/最早的日期并生成该日期和当前日期之间的每个月生成的。使用此数组,我想将其与第一个数组进行比较,以及当第一个数组中缺少日期时,然后将缺少的日期以及值"0"添加到其中。

所以完成的数组看起来像这样:

Array
(
    [Recycling] => Array
        (
            [May 14] => 7040
            [Jun 14] => 0
            [Jul 14] => 3920
            [Aug 14] => 14560
            [Sep 14] => 15120
            [Oct 14] => 12420
            [Nov 14] => 13440
            [Dec 14] => 13720
            [Jan 15] => 3920
        )
    [Disposal (Landfill)] => Array
        (
            [May 14] => 3800
            [Jun 14] => 7820
            [Jul 14] => 8100
            [Aug 14] => 5520
            [Sep 14] => 0
            [Oct 14] => 0
            [Nov 14] => 0
            [Dec 14] => 0
            [Jan 15] => 0
        )
    [Energy Recovery] => Array
        (
            [May 14] => 340
            [Jun 14] => 8500
            [Jul 14] => 6640
            [Aug 14] => 2860
            [Sep 14] => 7360
            [Oct 14] => 5380
            [Nov 14] => 8380
            [Dec 14] => 5440
            [Jan 15] => 1600
        )
)

我该怎么做?

编辑
这是查询。它是在Symfony2中构建的:

    $wasteHierarchyMonth = $dm->createQuery('
        SELECT SUM(efu.totalUom) AS totalTonne, efu.wasteHierarchy, efu.completionDate
        FROM CoreBundle:EnviroFiguresUpload efu
        WHERE efu.customerSite = :site
        AND efu.completionDate != :blank
        AND efu.wasteHierarchy != :blank
        GROUP BY efu.completionDate
        ORDER BY efu.completionDate ASC'
    )->setParameters(array(
        'site' => $cs,
        'blank' => ''
    ));
    $wasteHierMon = $wasteHierarchyMonth->getResult();

如果左联接不是一个选项,则必须手动填写字段。如果您愿意,可以应用此策略:

定义一个键数组并运行您公开的代码,就像您通常将值为 0 的所有有效日期存储在键数组中一样。然后遍历最终数组,并在键数组和存储的数组之间应用合并:

$whmOutput = array();
$keys = array();// Define array of keys.
foreach ($wasteHierMon as $whm) {// Run it (I guess you do) for the three of them.
    // Your code goes here.
    ....
    // Add this line at the end.
    $keys[$dateChanged] = 0; // Store the date and value 0 in the array called keys
}

为结果数组的不同部分运行脚本后,$keys将包含值为 0 的所有日期(无重复项)。现在,利用数组的键是字符串的情况,array_merge它:

如果输入数组具有相同的字符串键,则后面的值 因为该键将覆盖前一个键。

foreach($whmOutput as $key => $value) {
    $whmOutput[$key] = array_merge($keys, $value);// $keys goes first because you want to override values when there's a coincidence.
}

我还没有尝试过,但我认为它应该有效。