2013-01-19 29 views
1

我有一個使用highcharts的php頁面。我似乎無法讓xaxis使用數組填充。Highchart Array 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 ...而不是將其列出陣列中的日期。請幫忙。

回答

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 . ""; } ?>] 
}, 

這是未經測試的。

1

使用加入()函數陣列

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

實施例類別陣列:

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