Highchart Array Xaxis

时间:2013-01-19 22:28:44

标签: php highcharts

我有一个使用highcharts的php页面。我似乎无法使用数组填充xaxis。

我有一个使用以下代码制作的php数组:

$x = array(); 
while ($data = mysql_fetch_assoc($results)){ 
    $x[]= $data['sold_date'];  
}

当我打印_r时,我

  

数组([0] => 2009-01-20 [1] => 2009-04-17 [2] => 2009-09-15 [3] =>   2009-10-16 [4] => 2010-01-04 [5] => 2010-04-01 [6] => 2010-07-23 [7]   => 2010-10-20 [8] => 2011-01-07 [9] => 2011-05-27 [10] => 2011-07-01 [11] => 2011-10-14 [12] => 2012-01-27 [13] => 2012-04-25 [14] =>   2012-07-24 [15] => 2012-11-07 [16] => 2013-01-18)

现在在highcharts中我希望上面的数组是xaxis的值。我不知道我做错了什么。我试过了:

xAxis: {
  categories: ["<?php echo $x;?>"]
},

但它返回: 单词数组1 2 4 5 6 7 ...而不是列出数组中的日期。请帮忙。

2 个答案:

答案 0 :(得分:1)

使用join()函数来表示数组

categories: ['<?php echo join($categories, "', '") ?>']

示例类别数组:

<?php
   $categories[] = 'Jan';
   $categories[] = 'Feb';
   $categories[] = 'Mar';
   $categories[] = 'Apr';
?>

答案 1 :(得分:0)

<?php
$i = 0;
$num = count($x);
?>


xAxis: {
  categories: [<?php foreach($x as $key) { 
                         if(++$i === $num) { // this will remove the comma if last in array.
                             $comma = ''; 
                         }
                         else {
                             $comma = ',';
                         }
                         echo "'" . $key . "'" . $comma . ""; } ?>]
},

这是未经测试的。

相关问题