用于Highcharts的重组数组

时间:2013-03-07 12:51:58

标签: php highcharts highstock

我有多个数组遵循以下示例的格式,我想知道如何以编程方式重组数组以用于Highcharts(特别是Highstock)。我希望能够比较每个数组的数据,如this demo所示。[Date]应该是X轴,[Close]应该是给定数据点的Y轴。

数组示例:

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
        )
)

如果您需要更多信息或有任何疑问,请与我们联系。

由于

2 个答案:

答案 0 :(得分: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作为数据对象传递给图表。

答案 1 :(得分:0)

我不确定你究竟想要什么,

试试这个:

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

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