1

PHP() でフェッチしている MySQL データベースに複数の時系列があります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)"
        }
    }
}

これに関するいくつかの助けをいただければ幸いです!

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 配列を作成するにはループを作成する必要があります。おそらく次のようになります (データベース フィールドによって異なります)。

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 への直接的な 1 対 1 の変換ではありません (そのため、データを使用して最初に構造を作成し、次にその構造をエンコーダーに渡す必要があります)。

于 2012-10-17T19:58:31.777 に答える