首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为google图表生成json -添加空

为google图表生成json -添加空
EN

Stack Overflow用户
提问于 2014-01-01 00:10:14
回答 2查看 85关注 0票数 0

如果mysql返回的行中没有结果,我需要在json中生成一个空值。所谓“不存在”,我的意思是在$comps数组中没有一个或多个值的结果。

代码语言:javascript
复制
$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输出:

代码语言:javascript
复制
{"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}]}
]}

(我不担心最后一排假人)。问题在第二行。它应该是:

代码语言:javascript
复制
{"c":[{"v":"Date(2013, 11, 13)"},{"v":null},{"v":11},{"v":12}]},

如何避免插入这些额外的空号?

必须有更简单的方法来解决这件事,但我现在陷入了困境。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-01 21:03:21

我离得很近。这很管用..。

代码语言:javascript
复制
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;

}
票数 0
EN

Stack Overflow用户

发布于 2014-03-29 21:17:59

要从0到11获取google日期和月份,请尝试使用:

代码语言:javascript
复制
$date =new DateTime('2014-12-21');
$date->format('Y,n-1,d');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20864450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档