以下是我的代码。
我尝试使用以下数组绘制图表值。
在dates
数组中,我在数组中有30天。
我有另一个名为pending_date
和approved_date
的数组。
我需要匹配date.Add匹配键(日期)数组的键和值。你能告诉我哪里出错了吗?
$approved_date = array('2017-09-01'=>'1','2017-09-02' => '2', '2017-09-03' => '4');
$pending_date = array('2017-09-01'=>'2');
$rejected_date = array();
尝试过代码
$myYearMonth = date('Y-m');
$start = new DateTime(date('Y-m-01', strtotime($myYearMonth)));
$end = new DateTime(date('Y-m-t', strtotime($myYearMonth)).' +1 day');
$diff = DateInterval::createFromDateString('1 day');
$periodStart = new DatePeriod($start, $diff, $end);
foreach ( $periodStart as $dayDate ){
$dates[]['period'] = $dayDate->format( "Y-m-d" );
}
foreach( $pending_date as $key => $value ){
$data_key = array_search( $key, $dates );
if( $data_key !== false ) $dates[ $data_key ] = $value;
}
预期输出
{"period": "2017-09-01", "approved": 1, "pending": 2, "rejected": 0},
{"period": "2017-09-02", "approved": 2, "pending": 0, "rejected": 0},
{"period": "2017-09-03", "approved": 4, "pending": 0, "rejected": 0}
答案 0 :(得分:3)
尝试这样的事情:
$approved_date = array('2017-09-01'=>'1','2017-09-02' => '2', '2017-09-03' => '4');
$pending_date = array('2017-09-01'=>'2');
$rejected_date = array();
$dates = array();
//$periods = array_unique(array_merge(array_keys($approved_date), array_keys($pending_date), array_keys($rejected_date)));
$periods = array(); // fill $periods how you like
for($i=1; $i<=30; $i++) {
$periods[] = '2017-09-' . str_pad($i, 2, '0', STR_PAD_LEFT);
}
foreach ($periods as $period) {
$dates[] = array(
'period' => $period,
'approved' => isset($approved_date[$period]) ? $approved_date[$period] : 0,
'pending' => isset($pending_date[$period]) ? $pending_date[$period] : 0,
'rejected' => isset($rejected_date[$period]) ? $rejected_date[$period] : 0,
);
}
echo json_encode($dates);