如果从mysql返回的行中不存在结果,我需要在json中生成一个空值。 “不存在”,我的意思是$ comps数组中没有一个或多个值的结果。
$data = array(
'rows' => array()
);
// these values will change, based on submitted form
$comps = array( 'activity','purchase','groups');
$comps_count = count( $comps );
// mimic return from mysql
$sql_results = array (
array ( 'year' => '2013', 'month' => '12', 'day' => '7', 'component' => 'activity', 'num' => '10'),
array ('year' => '2013', 'month' => '12', 'day' => '13', 'component' => 'purchase', 'num' => '11'),
array ('year' => '2013', 'month' => '12', 'day' => '13', 'component' => 'groups', 'num' => '12'),
array ('year' => '2013', 'month' => '12', 'day' => '30', 'component' => 'activity', 'num' => '13'),
array ('year' => '2013', 'month' => '12', 'day' => '30', 'component' => 'groups', 'num' => '14'),
array ('year' => '2013', 'month' => '12', 'day' => '31', 'component' => 'dummy', 'num' => '0'),
);
$results = array();
foreach ( $sql_results as $row ) {
$results[] = (object)$row;
}
$j = 0;
foreach ( $results as $result ) {
$num = (int) $result->num;
$year = (int) $result->year;
$month = (int) $result->month - 1; // for javascript months
$day = (int) $result->day;
$this_date = $year . $month . $day;
$component = $result->component;
$key = array_search($component, $comps);
if ( $temp_date != $this_date ) {
if ( !empty( $temp_arr ) ) {
$diff = abs( ( $comps_count + 1 ) - count( $temp_arr ) );
if ( $diff != 0 ) {
for ( $i=0; $i<$diff; $i++ )
$temp_arr[] = array('v' => null);
}
$data['rows'][] = array('c' => $temp_arr);
$temp_arr = array();
$j = 0;
}
$temp_arr[] = array('v' => "Date($year, $month, $day)");
$j++;
for ( $i=0; $i<$key; $i++ ) {
$temp_arr[] = array('v' => null);
}
$temp_arr[] = array('v' => $num);
}
else {
$j++;
$xcount = $comps_count - $key;
if ( $xcount != $j ) {
for ( $i=$xcount; $i<$key; $i++ ) {
$temp_arr[] = array('v' => null);
}
}
$temp_arr[] = array('v' => $num);
}
$temp_date = $this_date;
}
$data['rows'][] = array('c' => $temp_arr);
$json = json_encode($data, JSON_NUMERIC_CHECK);
echo $json;
json输出:
{"rows":[
{"c":[{"v":"Date(2013, 11, 7)"},{"v":10},{"v":null},{"v":null}]},
{"c":[{"v":"Date(2013, 11, 13)"},{"v":null},{"v":11},{"v":null},{"v":12},{"v":null}]},
{"c":[{"v":"Date(2013, 11, 30)"},{"v":13},{"v":null},{"v":14}]},
{"c":[{"v":"Date(2013, 11, 31)"},{"v":0}]}
]}
(我并不担心最后一个虚拟行)。 问题出在第二个json行。它应该是:
{"c":[{"v":"Date(2013, 11, 13)"},{"v":null},{"v":11},{"v":12}]},
如何避免插入这些额外的空值?
并且 - 必须有一种更简单的方法来解决这个问题,但我现在处于脑力锁定状态。
答案 0 :(得分:0)
我很亲密。这有效......
foreach ( $results as $result ) {
$num = (int) $result->num;
$year = (int) $result->year;
$month = (int) $result->month - 1; // for javascript months
$day = (int) $result->day;
$this_date = $year . $month . $day;
$component = $result->component;
$key = array_search($component, $comps);
if ( $temp_date != $this_date ) {
if ( !empty( $temp_arr ) ) {
$diff = ( $comps_count + 1 ) - count( $temp_arr );
if ( $diff != 0 ) {
for ( $i=0; $i<$diff; $i++ ) {
$temp_arr[] = array('v' => null);
}
}
$data['rows'][] = array('c' => $temp_arr);
$temp_arr = array();
}
$temp_arr[] = array('v' => "Date($year, $month, $day)");
for ( $i=0; $i<$key; $i++ ) {
$temp_arr[] = array('v' => null);
}
$temp_arr[] = array('v' => $num);
}
else {
$diff = ( $key + 1 ) - count( $temp_arr );
if ( $diff != 0 )
$temp_arr[] = array('v' => null);
$temp_arr[] = array('v' => $num);
}
$temp_date = $this_date;
}
答案 1 :(得分:0)
要获取谷歌日期从0到11的月份,请尝试使用:
$date =new DateTime('2014-12-21');
$date->format('Y,n-1,d');