1

我在使用 PHP( fetch_assoc) 获取的 MySQL 数据库中有多个时间序列。

每个系列的 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)"
        }
    }
}

对此的一些帮助将不胜感激!

4

2 回答 2

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 数组,可能是这样的(取决于你的 db 字段):

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

于 2012-10-17T20:25:34.163 回答
1

看看json_encode/json_decode因为它会做你正在寻找的东西。

翻译:

  • JSON=>PHP
    json_decode数据和 PHP 将使用相同的大纲创建结构。
  • PHP=>JSON 使用 PHP 对象创建结构,然后调用json_encode以输出信息。

如果在编码之前需要进行一些操作,则必须这样做。从您问题的措辞来看,数据库不是直接一对一的 JSON 转换(因此您需要先使用数据创建结构,然后将该结构传递给编码器)。

于 2012-10-17T19:58:31.777 回答