title: PHP array into google charts
body: <p>I need a little help putting PHP data into google charts.</p>
  
  <p>I have created a simple array</p>
  
  <pre><code>$chart_arr = array($year, $new_balance);
  json_encode($chart_arr);    
  </code></pre>
  
  <p>If I run</p>
  
  <pre><code>&lt;?php echo json_encode($chart_arr);?&gt;    
  </code></pre>
  
  <p>I see the following: [2015,1150] [2016,1304.5] [2017,1463.635] [2018,1627.54405] [2019,1796.3703715], so I think (?) I am getting the right numbers encoded from my forloop that generates $year and $new_balance.</p>
  
  <p>I want to chart these numbers in google chart</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  
  function drawChart() {
  
  
  var data = new google.visualization.DataTable();
                  data.addColumn('string', 'Year');
                  data.addColumn('number', 'Balance');
                  data.addRows([
                      &lt;?php echo json_encode($chart_arr);?&gt;
                  ]);    
  </code></pre>
  
  <p>Or alternatively:</p>
  
  <pre><code>                data.addRows([
                  &lt;?php echo $chart_arr;?&gt;
              ]);    
  </code></pre>
  
  <p>And then continues...</p>
  
  <pre><code>var options = {
                title: 'My Savings',
                curveType: 'function',
                legend: { position: 'bottom' } 
  };
  
  var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
  chart.draw(data, options);
  </code></pre>
  
  <p>}
     </p>
  
  <p>displaying as...</p>
  
  <pre><code>    &lt;div class="grid-container"&gt; 
      &lt;div class="grid-100 grid-parent"&gt;
          &lt;div id="curve_chart" style="width: 100%; height: auto"&gt;&lt;/div&gt;
       &lt;/div&gt;   
  
  &lt;/div&gt;
  </code></pre>
  
  <p>I have tried a number of variations but am either not getting the data into the chart or not getting a chart to display. </p>
  
  <p>Could someone help me to show where I am going wrong?</p>
  
  <p>I saw another related post that used the following code:</p>
  
  <pre><code>     $chartsdata[$i] = array($testTime, $testNb);
  echo json_encode($chartsdata);
  
  var jsonData = $.ajax({
          url: "test.php",
          dataType: "json",
          async: false
      }).responseText;
  
      var obj = JSON.stringify(jsonData);
      data.addRows(obj);
  </code></pre>
  
  <p>Is this the approach I need to be looking at?</p>
  
  <p>Thanks in advance</p>