首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >特定格式的PHP数组

特定格式的PHP数组
EN

Stack Overflow用户
提问于 2017-11-08 23:59:50
回答 1查看 51关注 0票数 0

我正在为google图表(日历图表)生成一个数组,JSON的预期格式如下:

代码语言:javascript
运行
复制
dataTable.addColumn({ type: 'date', id: 'Date' });
       dataTable.addColumn({ type: 'number', id: 'Won/Loss' });
       dataTable.addRows([
          [ new Date(2012, 3, 13), 37032 ],
          [ new Date(2012, 3, 14), 38024 ],
          [ new Date(2012, 3, 15), 38024 ],
          [ new Date(2012, 3, 16), 38108 ],
          [ new Date(2012, 3, 17), 38229 ],
          [ new Date(2013, 9, 4), 38177 ],
          [ new Date(2013, 9, 5), 38705 ],
          [ new Date(2013, 9, 12), 38210 ],
          [ new Date(2013, 9, 13), 38029 ],
          [ new Date(2013, 9, 19), 38823 ],
          [ new Date(2013, 9, 23), 38345 ],
          [ new Date(2013, 9, 24), 38436 ],
          [ new Date(2013, 9, 30), 38447 ]
        ]);

在从sql server DB检索数据时,我生成的格式如下:

代码语言:javascript
运行
复制
 {"cols":[{"type":"date","id":"PositionDate"},
{"type":"number","id":"SecurityID"}],
        "rows":[["2017, 8, 10",528228],
        ["2017, 9, 8",614800],
        ["2017, 9, 15",703806]]

以下是简短的代码:

代码语言:javascript
运行
复制
$numRows = sqlsrv_num_rows($result);
    do {                            
        while($row = sqlsrv_fetch_array($result))
        {
            $temp = array();
            $rows[] = array(date('Y, n, j', strtotime($row['PositionDate'])),$row['SecurityID']);
        }
    } while (sqlsrv_next_result($result));

    $table['rows'] = $rows;

有人能指导我在JSON google图表中生成预期的格式吗?

谢谢。Zufliqar

EN

回答 1

Stack Overflow用户

发布于 2017-11-09 19:32:39

成功地完成了以上工作:

代码语言:javascript
运行
复制
$numRows = sqlsrv_num_rows($result);
    do {                            
        while($row = sqlsrv_fetch_array($result))
        {
            $temp = array();
            $temp[] = array('v' => 'Date('.date('Y',strtotime($row['LastUpdate'])).','.(date('n',strtotime($row['LastUpdate'])) - 1).','.date('d',strtotime($row['LastUpdate'])).','.date('H',strtotime($row['LastUpdate'])).','.date('i',strtotime($row['LastUpdate'])).','.date('s',strtotime($row['LastUpdate'])).')');
            $temp[] = array('v' => 'Date('.date('Y',strtotime($row['PositionDate'])).','.(date('n',strtotime($row['PositionDate'])) - 1).','.date('d',strtotime($row['PositionDate'])).','.date('H',strtotime($row['PositionDate'])).','.date('i',strtotime($row['PositionDate'])).','.date('s',strtotime($row['PositionDate'])).')');
            $temp[] = array('v' => "SecurityID {$row['SecurityID']} was last updated at {$row['LastUpdate']} and has position date of {$row['PositionDate']}");
            $rows[] = array('c' => $temp);
        }
    } while (sqlsrv_next_result($result));

    $table['rows'] = $rows;

Javascript/JSON,如下所示:

代码语言:javascript
运行
复制
// Load the Visualization API and the piechart package.
    google.charts.load("current", {packages:["corechart"]});

    // Set a callback to run when the Google Visualization API is loaded.
    google.charts.setOnLoadCallback(drawChart);

    function drawChart() {
               // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonData?>);

      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

      var options = {
        vAxis: { format: 'yyyy-M-d', title: 'PositionDate'},
        hAxis: { format: 'yyyy-M-d', title: 'LastUpdate'},
        title: "Stale ETFs",
         height: 350,
         tooltip: {isHtml: true},
         legend: 'none'
        };
      chart.draw(data, options);
    }

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47184223

复制
相关文章

相似问题

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