我给出了一个数组数组,以简化每个数组
[0] => Array
(
[target_id]
[target_date]
[intensity_id]
[target_intensity]
)
[1] => Array
(
[target_id]
[target_date]
[intensity_id]
[target_intensity]
)
示例数据(原始数组是一个关联数组)
target_id target_date intensity_id target_intensity
1829 2017-06-23 2 good
2013 2017-05-22 1 bad
2024 2017-05-18 1 bad
2029 2017-05-14 1 bad
2032 2017-05-10 3 bad
2329 2017-03-23 2 good
2629 2017-03-12 3 excellent
2830 2017-02-03 2 good
3829 2017-02-23 2 good
4029 2017-02-18 1 bad
3829 2017-01-23 2 good
3829 2016-12-12 3 excellent
3829 2016-12-23 2 good
现在我需要回应这些数据作为谷歌堆积条形图的输入。
所以我需要按月计算target_intensity的总和(强度出现在一个月内的次数),如此
date bad good excellent
2016 dec 0 1 1
2017 jan 0 1 0
2017 feb 1 2 0
2017 mrt 0 1 1
2017 may 3 0 1
2017 jun 0 1 0
这里我被困了
$tmpArray = array(
array('target_id' => 1829, 'target_date' => '2017-06-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 2013, 'target_date' => '2017-05-22', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 2024, 'target_date' => '2017-05-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 2029, 'target_date' => '2017-05-14', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 2032, 'target_date' => '2017-05-10', 'intensity_id'=>3, 'target_intensity'=>'bad'),
array('target_id' => 2329, 'target_date' => '2017-03-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 2629, 'target_date' => '2017-03-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
array('target_id' => 2830, 'target_date' => '2017-02-03', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 3829, 'target_date' => '2017-02-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 4029, 'target_date' => '2017-02-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 4039, 'target_date' => '2017-01-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 4049, 'target_date' => '2016-12-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
array('target_id' => 4056, 'target_date' => '2016-12-23', 'intensity_id'=>2, 'target_intensity'=>'good')
);
//echo $tmpArray[3]['target_date'];
//exit();
// should i first loop and order by month and then
//should i create 3 tmp arrays for bad, good and excellent
foreach ($tmpArray as $key => $values) {
echo $values['target_date'] . '<br/>';
//print_r ($values).'<br/>';
foreach ($values as $key => $value) {
// echo '<pre>';
echo $key . ' - ' . $value . '<br/>';
// echo '</pre>';
}
echo '<hr/>';
}
任何帮助表示赞赏
答案 0 :(得分:1)
希望这会对你有所帮助:我只是将结果分组以制作数组
<?php
$array = array(
array('target_id' => 1829, 'target_date' => '2017-06-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 2013, 'target_date' => '2017-05-22', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 2024, 'target_date' => '2017-05-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 2029, 'target_date' => '2017-05-14', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 2032, 'target_date' => '2017-05-10', 'intensity_id'=>3, 'target_intensity'=>'bad'),
array('target_id' => 2329, 'target_date' => '2017-03-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 2629, 'target_date' => '2017-03-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
array('target_id' => 2830, 'target_date' => '2017-02-03', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 3829, 'target_date' => '2017-02-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 4029, 'target_date' => '2017-02-18', 'intensity_id'=>1, 'target_intensity'=>'bad'),
array('target_id' => 4039, 'target_date' => '2017-01-23', 'intensity_id'=>2, 'target_intensity'=>'good'),
array('target_id' => 4049, 'target_date' => '2016-12-12', 'intensity_id'=>3, 'target_intensity'=>'excellent'),
array('target_id' => 4056, 'target_date' => '2016-12-23', 'intensity_id'=>2, 'target_intensity'=>'good')
);
$output = [];
foreach($array as $value) {
$year = date('Y', strtotime($value['target_date']));
$month = date('M', strtotime($value['target_date']));
if(empty($output[$year][$month]))
$output[$year][$month] = ['bad' => 0, 'good' => 0, 'excellent' => 0];
$output[$year][$month][$value['target_intensity']] += 1;
}
ksort($output);
ksort($output[$year]);
echo "<pre>";
print_r($output);
?>
答案 1 :(得分:0)
基本上有两个步骤要做,按年/月分组数据并计算实际总和。为了分组,您需要一个通用标识符(只需从输入中删除一天)。总和基本上是分组功能。
$tmpArray = array_map(function($target) {
$target['target_date'] = substr($target['target_date'], 0, 7);
return $target;
}, $tmpArray);
$finalData = array_reduce($tmpArray, function($carry, $target) {
if(!isset($carry[$target['target_date']])) {
$carry[$target['target_date']] = ['bad' => 0, 'good' => 0, 'excellent' => 0];
}
$carry[$target['target_date']][$target['target_intensity']]++;
return $carry;
}, []);