du2229 2014-07-28 09:38
浏览 7
已采纳

新的日期到数组PHP

For Google Visualization I coded the JSON by hand (Messy I know) I did not think about ''s in NAME_TEAM which has recently come about an issue as my first team name was entered with an apostrophe. I have read this post and tried to implement php json_encode. I am having an issue with the dates. The new Date works fine in the old messy json format, however I get an error Fatal error: Class 'Date' when I try use anything similar for the Json_encode version. The format must be date for the visualization api to understand the values.

foreach ($data as $row) {
$dateArray = explode('-', $row['THE_DATE']);
$year = $dateArray[0];
$month = $dateArray[1] - 1;
$day = $dateArray[2];
$dataArray[] = "[new Date ($year, $month, $day),  {$row['ORANGE']}, {$row['GREEN']}, '{$row['NAME_TEAM']}']";


$itemOutput[] = array(
new Date($year, $month, $day),
$row['ORANGE_SCORE'],
$row['GREEN_SCORE'],
$row['NAME_TEAM'],
);
}
echo "data.addRows(" . json_encode($itemOutput) . ");" ;

Data Table creation

  function drawDashboard() {
         //var data = new google.visualization.DataTable();
          var data = new google.visualization.DataTable(<?php echo json_encode($itemOutput, JSON_NUMERIC_CHECK); ?>);
            data.addColumn('date', 'Date');
            data.addColumn('number', 'ORANGE_SCORE');
            data.addColumn('number', 'GREEN_SCORE');
            data.addColumn('string', 'NAME_TEAM');

       //   data.addRows(<?php echo '[' . implode(',', $dataArray) . ']'; ?>);

ARRAY FORMAT

{"c":[
   {"Date":"new Date(2013, 9, 19)"},
   {"ORANGE_SCORE":14},
   {"GREEN_SCORE":7},
   {"NAME_TEAM":"Trigon"}]
},
  • 写回答

2条回答 默认 最新

  • dongxibo2095 2014-07-28 14:35
    关注

    You can't use Date objects in JSON; the only way to transmit dates is via strings, but the DataTable.addRows method does not parse strings into Date objects, so you will have to convert to the full JSON structure to make this work. Here's an example:

    $itemOutput = array(
        'cols' => array(
            array('type' => 'date', 'label' => 'Date'),
            array('type' => 'number', 'label' => 'ORANGE_SCORE'),
            array('type' => 'number', 'label' => 'GREEN_SCORE'),
            array('type' => 'string', 'label' => 'NAME_TEAM')
        ),
        'rows' => array()
    );
    
    foreach ($data as $row) {
        $dateArray = explode('-', $row['THE_DATE']);
        $year = $dateArray[0];
        $month = $dateArray[1] - 1;
        $day = $dateArray[2];
    
        $itemOutput['rows'][] = array('c' => array(
            array('v' => "Date($year, $month, $day)"),
            array('v' => $row['ORANGE_SCORE']),
            array('v' => $row['GREEN_SCORE']),
            array('v' => $row['NAME_TEAM'])
        ));
    }
    

    Then in javascript:

    var data = new google.visualization.DataTable(<?php echo json_encode($itemOutput, JSON_NUMERIC_CHECK); ?>);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog