2015-05-25 53 views
0

如何从阵列中获取这些数据到Highcharts?PHP阵列到Highcharts

[0] => Array 
     (
      [0] => Date 
      [1] => data1 
      [2] => data2 
     ) 

    [1] => Array 
     (
      [0] => 2015-03-26 02:00 
      [1] =>  1,425 
      [2] =>  39,294 
     ) 

    [2] => Array 
     (
      [0] => 2015-03-26 03:00 
      [1] =>  1,422 
      [2] =>  39,076 
     ) 

我是新来的PHP,并寻找一种方式来获得的第一个键(日期)为x轴,KEY2(数据2)为y轴。可以通过选择数组中的所有键值并将其传递给Highcharts的此JavaScript?

$(function() { 
      $('#container').highcharts({ 
       chart: { 
        type: 'line' 
       }, 
       title: { 
        text: 'Chart Title' 
       }, 
       xAxis: { 
        categories: //Dates here 2015-03-26 02:00// 
       }, 
       yAxis: { 
        title: { 
         text: 'a text here' 
        } 
       }, 
       series: [{ 
        name: 'data1', 
        data: // key1 from array 1,425 // 
       }, { 
        name: 'data2', 
        data: // key2 from array 39,294 // 
       }] 
      }); 
     }); 

回答

0
$data = array 
(
    array 
    (
     "Date", 
     "data1", 
     "data2" 
    ), 
    array 
    (
     "2015-03-26 02:00", 
     "1,425", 
     "39,294" 
    ), 
    array 
    (
     "2015-03-26 03:00", 
     "1,422", 
     "39,076" 
    ) 
); 

$series = array(); 

// Get the items of the first array 
$items = $data[0]; 

foreach($items as $itemIndex => $value) 
{ 
    // Skip the 'Date' 
    if (!$itemIndex) 
     continue; 

    // We should have the actual array to add to the 'data', if not skip 
    if (empty($data[$itemIndex])) 
     continue; 

    $series[] = array 
    (
     "name" => $value, 
     "data" => $data[$itemIndex] 
    ); 
} 

echo json_encode($series); 
+0

谢谢你,你会怎么把名字和数据数组字段到JavaScript的? –

+0

这个名字是在连续数组中,你能指定你想添加的名字吗? –

+0

我更新了我的第一篇文章。我想将日期传递给xAxis,并将data1传递给第一个系列。 data2到第二个系列。 –