我有这个数组:
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'];
}
我遇到的问题是数组的第二级部分不会总是有一个值(可以在数组的Recycling
和Disposal
部分看到。
我有另一个使用此代码的数组:
$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
)
通过查找第一个数组中的最低/最旧日期并生成该日期与当前日期之间的每个月来生成此数组。使用此数组,我想将它与第一个数组进行比较,并在第一个数组中缺少日期时,然后添加缺少的日期以及值&#39; 0&#39; 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 :(得分:0)
如果左连接不是一个选项,则您必须手动填写字段。如果您愿意,可以应用此策略:
定义一个键数组并运行您公开的代码,因为您通常在键的数组中存储所有有效日期值为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.
}
我还没有尝试过,但我认为它应该可行。