Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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从api.met.no读取天气提要_Php_Json - Fatal编程技术网

PHP从api.met.no读取天气提要

PHP从api.met.no读取天气提要,php,json,Php,Json,我正试图从中读取JSON 但这当然对我不起作用。请你不知道怎么做?感谢您查看原始json返回,您试图获取的项目已被完全掩埋,但您仍然可以轻松获取。数据返回示例: { "type": "Feature", "geometry": { ... }, "properties": { "meta": { ... }, "timese

我正试图从中读取JSON


但这当然对我不起作用。请你不知道怎么做?感谢您查看原始json返回,您试图获取的项目已被完全掩埋,但您仍然可以轻松获取。数据返回示例:

{
  "type": "Feature",
  "geometry": {
     ...
  },
  "properties": {
    "meta": {
       ...
    },
    "timeseries": [
        {
        "time": "2020-08-18T12:00:00Z",
        "data": {
          "instant": {
            "details": {
              "air_pressure_at_sea_level": 1018.4,
              "air_temperature": 4.7,
              "cloud_area_fraction": 92.2,
              "relative_humidity": 59.3,
              "wind_from_direction": 308.4,
              "wind_speed": 3.8
            }
         },
    ...

您可以通过以下方式访问该特定数据:

$json = file_get_contents($data_url,false,$context);

// This is all you need to turn json into a usable array
$data = json_decode($json,true); 

// Loop on the nested timeseries group:
foreach($data['properties']['timeseries'] as $ts) {
    
    // the time part of it
    $time    = $ts['time'];
    
    // get at it direct
    $var     = $ts['data']['instant']['details']['air_temperature'];

    // shorthand it if you wish:
    $details = $ts['data']['instant']['details'];
    $var     = $details['air_temperature'];
    
    // do whatever else you need to do with it
    echo $var;
    $array_of_temps[] = $var;
    // etc ...

}


对不起,我误解了。我需要从[“属性”][“时间序列”][“0-x”][“数据”][“即时”][“详细信息”]获取值。。。按你写的方式,它不起作用。事情是这样的,但我必须分别列出每一个:``echo$data['properties'][“timeseries”][“0”][“data”][“instant”][“details”];echo$data['properties'][“timeseries”][“1”][“data”][“instant”][“details”]```如果访问
$data['properties'][“timeseries”][“0”][“data”][“instant”][“details”][“details”][/code>,
$data['properties'][“timeseries”][“1”][“data”][“instant”][“details”][“details”][/code>等,你就会得到你想要的东西,那么你应该在哪一个层次上进行循环就很明显了——没有?那么答案的改变有助于解决你的问题吗?
{
  "type": "Feature",
  "geometry": {
     ...
  },
  "properties": {
    "meta": {
       ...
    },
    "timeseries": [
        {
        "time": "2020-08-18T12:00:00Z",
        "data": {
          "instant": {
            "details": {
              "air_pressure_at_sea_level": 1018.4,
              "air_temperature": 4.7,
              "cloud_area_fraction": 92.2,
              "relative_humidity": 59.3,
              "wind_from_direction": 308.4,
              "wind_speed": 3.8
            }
         },
    ...

$json = file_get_contents($data_url,false,$context);

// This is all you need to turn json into a usable array
$data = json_decode($json,true); 

// Loop on the nested timeseries group:
foreach($data['properties']['timeseries'] as $ts) {
    
    // the time part of it
    $time    = $ts['time'];
    
    // get at it direct
    $var     = $ts['data']['instant']['details']['air_temperature'];

    // shorthand it if you wish:
    $details = $ts['data']['instant']['details'];
    $var     = $details['air_temperature'];
    
    // do whatever else you need to do with it
    echo $var;
    $array_of_temps[] = $var;
    // etc ...

}