使用数组填充google.visualization.arrayToDataTable

时间:2017-09-29 15:55:54

标签: php google-visualization

我是PHP和Google图表的新用户。在此先感谢您的帮助。

我想获得转换数组$votedata的帮助,我填写下面的代码:

array_push(
    $votedata,
    array(
        "Plats" => $name,
        "Bra" => $val1,
        "Ok" => $val2,
        "Neutral" => $val3,
        "Dålig" => $val4
    )
);

如果我print_r($votedata);它看起来如下,那么猜它可以:

Array ( 
    [0] => Array ( [Plats] => AAA [Bra] => 4 [Ok] => 0 [Neutral] => 2 [Dålig] => 0 ) 
    [1] => Array ( [Plats] => BBB [Bra] => 7 [Ok] => 0 [Neutral] => 1 [Dålig] => 2 ) 
    [2] => Array ( [Plats] => CCC [Bra] => 2 [Ok] => 3 [Neutral] => 1 [Dålig] => 6 )
)

如何将其转换为使用以下代码?

function drawChart() {
    var data = google.visualization.arrayToDataTable([
      //<?php
      //    Convert and use $votedata;
      //?>
    ]);
...

1 个答案:

答案 0 :(得分:0)

您可以使用foreach打印出每一行......

function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ["Plats", "Bra", "Ok", "Neutral", "Dålig"],

  <?php
    foreach ($votedata as $row) {
  ?>

    [<?="'".$row['Plats']."'"?>, <?=$row['Bra']?>, <?=$row['Ok']?>, <?=$row['Neutral']?>, <?=$row['Dålig']?>],

  <?php
    }
  ?>

  ]);
...