我有一个格式如下:
Array
(
[Jan 14] => Array
(
[wasteHierarchy] => Recycling
[completionDate] => Jan 14
[totalUom] => 102854
)
[Feb 14] => Array
(
[wasteHierarchy] => Recycling
[completionDate] => Feb 14
[totalUom] => 83954
)
[Mar 14] => Array
(
[wasteHierarchy] => Landfill
[completionDate] => Mar 14
[totalUom] => 97506
)
)
但是我需要改变数组的布局方式。我想创建一个这种格式的数组:
Array
(
[Recycling] => Array
(
[Jan 14] => 102854
[Feb 14] => 83954
)
[Landfill] => Array
(
[Mar 14] => 97506
)
)
这是我目前的代码:
$whmOutput = Array();
foreach($wasteHierMonArr as $whm) {
$dateChanged = date("M y", strtotime($whm['completion_date']));
$whmOutput_element = &$whmOutput[$dateChanged];
$whmOutput_element['wasteHierarchy'] = $whm['waste_hierarchy'];
$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;
}
但是,这只返回一个数组:
Array
(
[Recycling] => Array
(
[Jan 14] => 102854
[Feb 14] => 83954
)
)
我做错了什么?
修改
$wasteHierMonArr
是从SQL语句生成的:
SELECT SUM(total_uom) AS totalTonne, waste_hierarchy, completion_date
FROM enviro_figures_upload
WHERE completion_date != ''
AND waste_hierarchy != ''
GROUP BY waste_hierarchy, DATE_FORMAT(completion_date, '%m-%Y') DESC
ORDER BY YEAR(completion_date) ASC, MONTH(completion_date) ASC, waste_hierarchy ASC
答案 0 :(得分:0)
尝试:
foreach($wasteHierMonArr as $datekey => $dta){
$newWHM[$dta['wasteHierarchy']][$datekey] = $dta['totalUom];
}
答案 1 :(得分:0)
<?php
$whmOutput = Array();
foreach ($wasteHierMonArr as $whm) {
$whmOutput[$whm['wasteHeirarchy']][$whm['completionDate']] = $whm['totalUom'];
}
答案 2 :(得分:0)
$result = [];
foreach ($wasteHierMonArr as $data) {
$result[$data['wasteHierarchy']][$data['completionDate']] = $data['totalUom'];
}
print_r($result);