2012-10-17 34 views
1

我有一個MySQL數據庫中的多個時間序列,我用PHP (fetch_assoc)獲取。如何從我的數據在PHP中創建這個JSON結構?

每個系列具有相同的X軸但Y軸不同。

X軸datetime(POSIX值)。

Y軸

air_temperature 
dew_point_temperature 
sea_level_pressure 
wind_direction 
wind_speed_rate 
sky_condition_total_coverage_code 
liquid_precipitation_depth_dimension_one_hr 
liquid_precipitation_depth_dimension_six_hr 

我需要輸出這個數據在特定的JSON結構。 這裏是正確的最終結果的一個例子:

{ "firstRow" : { "beginTime" : "2012-10-09 00:00:01", 
     "endTime" : "2012-10-10 00:00:00", 
     "tMax" : "56.0", 
     "tMean" : "52.5", 
     "tMin" : "49.0" 
    }, 
    "interval" : "daily", 
    "lastRow" : { "beginTime" : "2012-10-15 00:00:01", 
     "endTime" : "2012-10-16 00:00:00", 
     "tMax" : "72.0", 
     "tMean" : "64.0", 
     "tMin" : "56.0" 
    }, 
    "series" : [ { "color" : "#FFAE28", 
     "data" : [ [ 1349740801000, 
       56 
      ], 
      [ 1349827201000, 
       60 
      ], 
      [ 1349913601000, 
       69 
      ], 
      [ 1350000001000, 
       61 
      ], 
      [ 1350086401000, 
       57 
      ], 
      [ 1350172801000, 
       56 
      ], 
      [ 1350259201000, 
       72 
      ] 
      ], 
     "name" : "Maximum Temperature (ºF)", 
     "type" : "spline", 
     "yAxis" : 0, 
     "zIndex" : 100 
     }, 
     { "color" : "#4bf827", 
     "data" : [ [ 1349740801000, 
       52.5 
      ], 
      [ 1349827201000, 
       56 
      ], 
      [ 1349913601000, 
       59 
      ], 
      [ 1350000001000, 
       55.5 
      ], 
      [ 1350086401000, 
       49.5 
      ], 
      [ 1350172801000, 
       49.5 
      ], 
      [ 1350259201000, 
       64 
      ] 
      ], 
     "name" : "Mean Temperature (ºF)", 
     "type" : "spline", 
     "yAxis" : 0, 
     "zIndex" : 100 
     }, 
     { "color" : "#2dc1f0", 
     "data" : [ [ 1349740801000, 
       49 
      ], 
      [ 1349827201000, 
       52 
      ], 
      [ 1349913601000, 
       49 
      ], 
      [ 1350000001000, 
       50 
      ], 
      [ 1350086401000, 
       42 
      ], 
      [ 1350172801000, 
       43 
      ], 
      [ 1350259201000, 
       56 
      ] 
      ], 
     "name" : "Minimum Temperature (ºF)", 
     "type" : "spline", 
     "yAxis" : 0, 
     "zIndex" : 100 
     } 
    ], 
    "title" : "New York Laguardia Arpt: Daily Temperature", 
    "xAxis" : { "max" : 1350259201000, 
     "maxZoom" : 604800000, 
     "min" : 1349740801000 
    }, 
    "yAxis" : { "endOnTick" : false, 
     "gridLineColor" : "#777", 
     "gridLineWidth" : 1, 
     "labels" : { "enabled" : true, 
      "style" : { "color" : "#eee" } 
     }, 
     "lineWidth" : 0, 
     "max" : null, 
     "maxPadding" : 0, 
     "min" : null, 
     "opposite" : false, 
     "startOnTick" : true, 
     "tickInterval" : null, 
     "title" : { "style" : { "color" : "#eee" }, 
      "text" : "Degrees (Fahrenheit)" 
     } 
    } 
} 

與此有些幫助將不勝感激!

+0

對PHP數組的基本理解可以讓你自己做這個。或者甚至做'print_r(json_decode('你的json字符串')); 1'。 –

+0

你需要通過json_encode($ array)將你的數組轉換爲json。 –

回答

2

您需要將數據庫中的數據轉換爲具有與您想要的javascript表示形式相同結構的php數組。然後,您可以使用json_encode($arr_data)創建JavaScript表示。

換句話說,你的$ arr_data必須結束了與此類似:

$arr_data = array(
    "firstRow" => array(
        "beginTime" => "2012-10-09 00:00:01", 
        "endTime" => "2012-10-10 00:00:00", 
        "tMax" => "56.0", 
        "tMean" => "52.5", 
        "tMin" => "49.0" 
       ), 
    "interval" => "daily", 
    "lastRow" => array(
       "beginTime" => "2012-10-15 00:00:01", 
       "endTime" => "2012-10-16 00:00:00", 
       "tMax" => "72.0", 
       "tMean" => "64.0", 
       "tMin" => "56.0" 
       ), 
    "series" => array(
     array(
     "color" => "#FFAE28", 
     "data" => array(
        array(1349740801000, 56), 
        array(1349827201000, 60), 
        etc... 
       ), 
     "name" => "Maximum Temperature (ºF)", 
     "type" => "spline", 
     etc.... 
    ) 
    ) 
); 

所以,你必須(根據你的數據庫字段)寫一個循環來創建這個PHP數組,也許是這樣的:

if ($result = $mysqli->query($query)) { 
    $arr_data = array(); 
    $i = 0; 
    while ($row = $result->fetch_assoc()) { 
    $arr_firstRow = array(); 
    $arr_firstRow["beginTime"] = $row["beginTime"]; 
    $arr_firstRow["endTime"] = $row["endTime"]; 
    etc... 
    $arr_data[$i]["firstRow"] = $arr_firstRow; 
    $arr_data[$i]["interval"] = $row["interval"]; 
    etc... 
    $i++; 
    } 
} 

然後,您可以使用json_encode($arr_data)

1

看看json_encode/json_decode,因爲它會做你想要的。

翻譯:

  • JSON => PHP
    json_decode數據和PHP將使用相同的輪廓創建該結構。
  • PHP => JSON 使用PHP對象創建結構,然後調用json_encode來輸出信息。

如果需要進行某些操作才能對其進行編碼,則必須執行此操作。根據您的問題的措辭來判斷,數據庫不是直接1對1的JSON轉換(因此您需要先使用數據創建結構,然後將該結構傳遞給編碼器)。

相關問題