Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从我的数据在PHP中创建这个JSON结构?_Php_Mysql_Json - Fatal编程技术网

如何从我的数据在PHP中创建这个JSON结构?

如何从我的数据在PHP中创建这个JSON结构?,php,mysql,json,Php,Mysql,Json,我用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_pre

我用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)"
        }
    }
}
在此方面的一些帮助将不胜感激

看看/因为它会满足你的需求

翻译:

  • JSON=>PHP
    json\u decode
    数据和PHP将使用相同的大纲创建结构
  • PHP=>JSON 使用PHP对象创建结构,然后调用
    json\u encode
    输出信息

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

您需要将数据从数据库获取到一个php数组中,该数组的结构与您想要的javascript表示形式相同。然后可以使用
json\u encode($arr\u data)
创建javascript表示

换句话说,您的$arr_数据必须与以下类似:

$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字段):


然后,您可以使用
json\u encode($arr\u data)

对PHP中的数组有一个基本的了解,您就可以自己完成这项工作。或者甚至做
print_r(json_decode('your json string here'));1
。您需要通过json_encode($array)将数组转换为json;
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++;
  }
}