我在MySQL数据库中有多个时间序列,我正在使用PHP获取
(fetch_assoc
)。
每个系列具有相同的X轴但Y轴不同。
X轴:日期时间(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 :(得分:2)
您需要将数据库中的数据转换为php数组,其结构与您想要的javascript表示形式相同。然后,您可以使用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 :(得分:1)
请查看json_encode
/ json_decode
,因为它可以满足您的需求。
翻译:
json_decode
数据和PHP将使用相同的大纲创建结构。json_encode
输出信息。如果在编码之前需要进行某些操作,则必须执行此操作。根据您的问题的措辞判断,数据库不是JSON的直接1对1转换(因此您需要首先使用数据创建结构,然后将该结构传递给编码器)。