PHP数组采用特定格式

时间:2017-11-08 15:59:50

标签: php arrays json

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

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中检索数据时,我生成的格式如下:

 {"cols":[{"type":"date","id":"PositionDate"},
{"type":"number","id":"SecurityID"}],
        "rows":[["2017, 8, 10",528228],
        ["2017, 9, 8",614800],
        ["2017, 9, 15",703806]]

以下是简要代码:

$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谷歌图表中按预期生成格式吗?

谢谢。 Zufliqar

1 个答案:

答案 0 :(得分:0)

管理以实现以上目标:

$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如下:

// 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);
    }

enter image description here