1

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

我有一个使用以下方法制作的 php 数组:

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

当我 print_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... 而不是列出数组中的日期。请帮忙。

4

2 回答 2

1

使用 join() 函数进行数组

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

示例类别数组:

<?php
   $categories[] = 'Jan';
   $categories[] = 'Feb';
   $categories[] = 'Mar';
   $categories[] = 'Apr';
?>
于 2013-02-01T12:45:02.717 回答
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 . ""; } ?>]
},

这是未经测试的。

于 2013-01-19T22:57:18.647 回答