2015-05-25 86 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到第二個系列。 –