2013-03-07 34 views
3

我有多個數組遵循下面的例子的格式,我想知道我如何編程重構陣列用於Highcharts(特別是Highstock)。我希望能夠比較每個數組的數據,如this demo所示。[Date]應該是X軸,[Close]應該是給定數據點的Y軸。重構陣列爲Highcharts使用

陣列的例子:

Array 
(
    [0] => Array 
     (
      [Date] => 2013-03-06 
      [Open] => 3.79 
      [High] => 3.64 
      [Low] => 3.48 
      [Close] => 3.52 
      [Volume] => 22184500 
      [Adj Close] => 3.72 
     ) 

    [1] => Array 
     (
      [Date] => 2013-03-05 
      [Open] => 3.63 
      [High] => 3.05 
      [Low] => 3.28 
      [Close] => 3.54 
      [Volume] => 32987900 
      [Adj Close] => 3.14 
     ) 

    [2] => Array 
     (
      [Date] => 2013-03-04 
      [Open] => 3.50 
      [High] => 3.67 
      [Low] => 3.50 
      [Close] => 3.64 
      [Volume] => 47933200 
      [Adj Close] => 3.84 
     ) 
) 

請讓我知道如果你需要了解更多信息或有任何疑問。

感謝

+0

你想要的輸出是什麼? – 2013-03-07 12:57:27

+0

什麼是Highcharts最簡單的消費方式。我假設我需要的唯一值是[日期]和[關閉]值。 – 585connor 2013-03-07 13:01:27

回答

1

如果你做這樣的事情:

$i = 0; 
    foreach($your_array as $val){ 
     $res[$i][] = strtotime($val['Date']) * 1000; //sets the date as a javascript timestamp 
     $res[$i][] = (float)$val['Close']; //make sure it is formatted as a number not a string 
     $i++; 
    } 
    json_encode($res); 

你應該有json,您可以將其作爲數據對象傳遞到圖表中。

0

我不知道究竟你在找,

試試這個:

$res    = array(); 
foreach($your_array as $key=>$val){ 
    $res[$key]['Date'] = $val['Date']; 
    $res[$key]['Close'] = $val['Close']; 
} 

echo "<pre>"; 
print_r($res); 
+0

這對擺脫不相關的數據很有好處,但它只通過'$ your_array'返回一個循環。我希望它能夠通過與數組中的項目一樣多的循環。 – 585connor 2013-03-07 18:23:07

+0

哦。是的我的不好,我編輯了答案:) – 2013-03-08 04:21:00