Format data for Highcharts chart

2019-07-28 17:23发布

I'm trying to create Highcharts chart from mysql, I have code like this:

 xAxis: {
   categories: [<?php  echo  implode($column, ','); ?>]
 }

and column has been grabbed from mysql:

  $column = array();
  $column[] = $row['blabla'];

What I am actually getting is:

 xAxis: {
   categories: [data1,data2,data3,data4,data5]
 }

but it has to be like this:

 xAxis: {
   categories: ["data1","data2","data3","data4","data5"]
 }

How can I format it like that?

3条回答
一夜七次
2楼-- · 2019-07-28 18:00

json_encode will format your array of strings as a JSON object, including escaping, etc., which can then be passed into HighCharts with the following:

xAxis: {
    categories: <?php echo json_encode($column); ?>
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-28 18:01

Just input the data with the quotes.

$column[] = '"'.$row['blabla'].'"';

查看更多
萌系小妹纸
4楼-- · 2019-07-28 18:05

this will work too.

categories: ["<\?php echo implode($column, '","'); ?>"]

查看更多
登录 后发表回答