从复杂的嵌套JSON对象获取数据(使用PHP)

时间:2016-06-20 05:06:24

标签: php json parsing nested decode

我正试图获得最低最高温度的时间" 15:00:00 "从这个复杂的嵌套JSON数据。任何人都可以帮我解决如何提取数据的问题吗?

JSON数据如下所示:

{
    "city": {
        "id": 1609350,
        "name": "Bangkok",
        "coord": {
            "lon": 100.51667,
            "lat": 13.75
        },
        "country": "TH",
        "population": 0,
        "sys": {
            "population": 0
        }
    },
    "cod": "200",
    "message": 0.0169,
    "cnt": 38,
    "list": [{
        "dt": 1466402400,
        "main": {
            "temp": 311.79,
            "temp_min": 308.315,
            "temp_max": 311.79,
            "pressure": 1018.37,
            "sea_level": 1018.77,
            "grnd_level": 1018.37,
            "humidity": 61,
            "temp_kf": 3.47
        },
        "weather": [{
            "id": 801,
            "main": "Clouds",
            "description": "few clouds",
            "icon": "02d"
        }],
        "clouds": {
            "all": 24
        },
        "wind": {
            "speed": 2.14,
            "deg": 269.001
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-20 06:00:00"
    }, {
        "dt": 1466413200,
        "main": {
            "temp": 309.22,
            "temp_min": 306.611,
            "temp_max": 309.22,
            "pressure": 1016.83,
            "sea_level": 1017.28,
            "grnd_level": 1016.83,
            "humidity": 61,
            "temp_kf": 2.6
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10d"
        }],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 1.07,
            "deg": 132.503
        },
        "rain": {
            "3h": 1.88
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-20 09:00:00"
    }, {
        "dt": 1466424000,
        "main": {
            "temp": 307.17,
            "temp_min": 305.429,
            "temp_max": 307.17,
            "pressure": 1017.62,
            "sea_level": 1018.09,
            "grnd_level": 1017.62,
            "humidity": 59,
            "temp_kf": 1.74
        },
        "weather": [{
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 92
        },
        "wind": {
            "speed": 1.33,
            "deg": 153.501
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 12:00:00"
    }, {
        "dt": 1466434800,
        "main": {
            "temp": 303.96,
            "temp_min": 303.092,
            "temp_max": 303.96,
            "pressure": 1019.69,
            "sea_level": 1020.02,
            "grnd_level": 1019.69,
            "humidity": 66,
            "temp_kf": 0.87
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 76
        },
        "wind": {
            "speed": 4.26,
            "deg": 180.504
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 15:00:00"
    }, {
        "dt": 1466445600,
        "main": {
            "temp": 301.672,
            "temp_min": 301.672,
            "temp_max": 301.672,
            "pressure": 1019.5,
            "sea_level": 1019.9,
            "grnd_level": 1019.5,
            "humidity": 74,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03n"
        }],
        "clouds": {
            "all": 48
        },
        "wind": {
            "speed": 3.06,
            "deg": 163.501
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 18:00:00"
    }, {
        "dt": 1466456400,
        "main": {
            "temp": 300.708,
            "temp_min": 300.708,
            "temp_max": 300.708,
            "pressure": 1018.73,
            "sea_level": 1019.13,
            "grnd_level": 1018.73,
            "humidity": 82,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 68
        },
        "wind": {
            "speed": 1.66,
            "deg": 141.5
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 21:00:00"
    }, {
        "dt": 1466467200,
        "main": {
            "temp": 301.217,
            "temp_min": 301.217,
            "temp_max": 301.217,
            "pressure": 1020.18,
            "sea_level": 1020.61,
            "grnd_level": 1020.18,
            "humidity": 79,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04d"
        }],
        "clouds": {
            "all": 68
        },
        "wind": {
            "speed": 1.61,
            "deg": 91.0013
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 00:00:00"
    }, {
        "dt": 1466478000,
        "main": {
            "temp": 306.368,
            "temp_min": 306.368,
            "temp_max": 306.368,
            "pressure": 1020.81,
            "sea_level": 1021.3,
            "grnd_level": 1020.81,
            "humidity": 62,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 2.86,
            "deg": 298.001
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 03:00:00"
    }, {
        "dt": 1466488800,
        "main": {
            "temp": 308.988,
            "temp_min": 308.988,
            "temp_max": 308.988,
            "pressure": 1019.11,
            "sea_level": 1019.63,
            "grnd_level": 1019.11,
            "humidity": 49,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }],
        "clouds": {
            "all": 44
        },
        "wind": {
            "speed": 4.12,
            "deg": 266.505
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 06:00:00"
    }, {
        "dt": 1466499600,
        "main": {
            "temp": 305.343,
            "temp_min": 305.343,
            "temp_max": 305.343,
            "pressure": 1017.43,
            "sea_level": 1017.76,
            "grnd_level": 1017.43,
            "humidity": 67,
            "temp_kf": 0
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10d"
        }],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 3.87,
            "deg": 212.501
        },
        "rain": {
            "3h": 1.415
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 09:00:00"
    }, {
        "dt": 1466510400,
        "main": {
            "temp": 304.423,
            "temp_min": 304.423,
            "temp_max": 304.423,
            "pressure": 1018.4,
            "sea_level": 1018.97,
            "grnd_level": 1018.4,
            "humidity": 63,
            "temp_kf": 0
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }],
        "clouds": {
            "all": 92
        },
        "wind": {
            "speed": 2.05,
            "deg": 196.508
        },
        "rain": {
            "3h": 1.075
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 12:00:00"
    }, {
        "dt": 1466521200,
        "main": {
            "temp": 303.07,
            "temp_min": 303.07,
            "temp_max": 303.07,
            "pressure": 1020,
            "sea_level": 1020.58,
            "grnd_level": 1020,
            "humidity": 67,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 68
        },
        "wind": {
            "speed": 2.56,
            "deg": 178.501
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 15:00:00"
    }, {
        "dt": 1466532000,
        "main": {
            "temp": 300.893,
            "temp_min": 300.893,
            "temp_max": 300.893,
            "pressure": 1020.32,
            "sea_level": 1020.72,
            "grnd_level": 1020.32,
            "humidity": 80,
            "temp_kf": 0
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }],
        "clouds": {
            "all": 56
        },
        "wind": {
            "speed": 1.62,
            "deg": 242.5
        },
        "rain": {
            "3h": 1.735
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 18:00:00"
    }, {
        "dt": 1466542800,
        "main": {
            "temp": 300.177,
            "temp_min": 300.177,
            "temp_max": 300.177,
            "pressure": 1019.56,
            "sea_level": 1020.01,
            "grnd_level": 1019.56,
            "humidity": 79,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 56
        },
        "wind": {
            "speed": 1.11,
            "deg": 237.002
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 21:00:00"
    }, {
        "dt": 1466553600,
        "main": {
            "temp": 301.577,
            "temp_min": 301.577,
            "temp_max": 301.577,
            "pressure": 1020.42,
            "sea_level": 1020.8,
            "grnd_level": 1020.42,
            "humidity": 74,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }],
        "clouds": {
            "all": 48
        },
        "wind": {
            "speed": 2.11,
            "deg": 288.01
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-22 00:00:00"
    }]
}

3 个答案:

答案 0 :(得分:1)

  <?php
$data= '{
  "city": {
    "id": 1609350,
    "name": "Bangkok",
    "coord": {
      "lon": 100.51667,
      "lat": 13.75
    },
    "country": "TH",
    "population": 0,
    "sys": {
      "population": 0
    }
  },
  "cod": "200",
  "message": 0.0169,
  "cnt": 38,
  "list": [
    {
      "dt": 1466402400,
      "main": {
        "temp": 311.79,
        "temp_min": 308.315,
        "temp_max": 311.79,
        "pressure": 1018.37,
        "sea_level": 1018.77,
        "grnd_level": 1018.37,
        "humidity": 61,
        "temp_kf": 3.47
      },
      "weather": [
        {
          "id": 801,
          "main": "Clouds",
          "description": "few clouds",
          "icon": "02d"
        }
      ],
      "clouds": {
        "all": 24
      },
      "wind": {
        "speed": 2.14,
        "deg": 269.001
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-20 06:00:00"
    },
    {
      "dt": 1466413200,
      "main": {
        "temp": 309.22,
        "temp_min": 306.611,
        "temp_max": 309.22,
        "pressure": 1016.83,
        "sea_level": 1017.28,
        "grnd_level": 1016.83,
        "humidity": 61,
        "temp_kf": 2.6
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 1.07,
        "deg": 132.503
      },
      "rain": {
        "3h": 1.88
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-20 09:00:00"
    },
    {
      "dt": 1466424000,
      "main": {
        "temp": 307.17,
        "temp_min": 305.429,
        "temp_max": 307.17,
        "pressure": 1017.62,
        "sea_level": 1018.09,
        "grnd_level": 1017.62,
        "humidity": 59,
        "temp_kf": 1.74
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 92
      },
      "wind": {
        "speed": 1.33,
        "deg": 153.501
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 12:00:00"
    },
    {
      "dt": 1466434800,
      "main": {
        "temp": 303.96,
        "temp_min": 303.092,
        "temp_max": 303.96,
        "pressure": 1019.69,
        "sea_level": 1020.02,
        "grnd_level": 1019.69,
        "humidity": 66,
        "temp_kf": 0.87
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 76
      },
      "wind": {
        "speed": 4.26,
        "deg": 180.504
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 15:00:00"
    },
    {
      "dt": 1466445600,
      "main": {
        "temp": 301.672,
        "temp_min": 301.672,
        "temp_max": 301.672,
        "pressure": 1019.5,
        "sea_level": 1019.9,
        "grnd_level": 1019.5,
        "humidity": 74,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03n"
        }
      ],
      "clouds": {
        "all": 48
      },
      "wind": {
        "speed": 3.06,
        "deg": 163.501
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 18:00:00"
    },
    {
      "dt": 1466456400,
      "main": {
        "temp": 300.708,
        "temp_min": 300.708,
        "temp_max": 300.708,
        "pressure": 1018.73,
        "sea_level": 1019.13,
        "grnd_level": 1018.73,
        "humidity": 82,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 68
      },
      "wind": {
        "speed": 1.66,
        "deg": 141.5
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 21:00:00"
    },
    {
      "dt": 1466467200,
      "main": {
        "temp": 301.217,
        "temp_min": 301.217,
        "temp_max": 301.217,
        "pressure": 1020.18,
        "sea_level": 1020.61,
        "grnd_level": 1020.18,
        "humidity": 79,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 68
      },
      "wind": {
        "speed": 1.61,
        "deg": 91.0013
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 00:00:00"
    },
    {
      "dt": 1466478000,
      "main": {
        "temp": 306.368,
        "temp_min": 306.368,
        "temp_max": 306.368,
        "pressure": 1020.81,
        "sea_level": 1021.3,
        "grnd_level": 1020.81,
        "humidity": 62,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 2.86,
        "deg": 298.001
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 03:00:00"
    },
    {
      "dt": 1466488800,
      "main": {
        "temp": 308.988,
        "temp_min": 308.988,
        "temp_max": 308.988,
        "pressure": 1019.11,
        "sea_level": 1019.63,
        "grnd_level": 1019.11,
        "humidity": 49,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 44
      },
      "wind": {
        "speed": 4.12,
        "deg": 266.505
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 06:00:00"
    },
    {
      "dt": 1466499600,
      "main": {
        "temp": 305.343,
        "temp_min": 305.343,
        "temp_max": 305.343,
        "pressure": 1017.43,
        "sea_level": 1017.76,
        "grnd_level": 1017.43,
        "humidity": 67,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 3.87,
        "deg": 212.501
      },
      "rain": {
        "3h": 1.415
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 09:00:00"
    },
    {
      "dt": 1466510400,
      "main": {
        "temp": 304.423,
        "temp_min": 304.423,
        "temp_max": 304.423,
        "pressure": 1018.4,
        "sea_level": 1018.97,
        "grnd_level": 1018.4,
        "humidity": 63,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 92
      },
      "wind": {
        "speed": 2.05,
        "deg": 196.508
      },
      "rain": {
        "3h": 1.075
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 12:00:00"
    },
    {
      "dt": 1466521200,
      "main": {
        "temp": 303.07,
        "temp_min": 303.07,
        "temp_max": 303.07,
        "pressure": 1020,
        "sea_level": 1020.58,
        "grnd_level": 1020,
        "humidity": 67,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 68
      },
      "wind": {
        "speed": 2.56,
        "deg": 178.501
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 15:00:00"
    },
    {
      "dt": 1466532000,
      "main": {
        "temp": 300.893,
        "temp_min": 300.893,
        "temp_max": 300.893,
        "pressure": 1020.32,
        "sea_level": 1020.72,
        "grnd_level": 1020.32,
        "humidity": 80,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 56
      },
      "wind": {
        "speed": 1.62,
        "deg": 242.5
      },
      "rain": {
        "3h": 1.735
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 18:00:00"
    },
    {
      "dt": 1466542800,
      "main": {
        "temp": 300.177,
        "temp_min": 300.177,
        "temp_max": 300.177,
        "pressure": 1019.56,
        "sea_level": 1020.01,
        "grnd_level": 1019.56,
        "humidity": 79,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 56
      },
      "wind": {
        "speed": 1.11,
        "deg": 237.002
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 21:00:00"
    },
    {
      "dt": 1466553600,
      "main": {
        "temp": 301.577,
        "temp_min": 301.577,
        "temp_max": 301.577,
        "pressure": 1020.42,
        "sea_level": 1020.8,
        "grnd_level": 1020.42,
        "humidity": 74,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 48
      },
      "wind": {
        "speed": 2.11,
        "deg": 288.01
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-22 00:00:00"
    }
  ]
}';

//decoding string into json object
$json=json_decode($data,TRUE);
$list=$json['list'];
for($i=0;$i<sizeof($list);$i++)
{
    $dt_time=$list[$i]['dt_txt'];
    $date = date('Y-m-d',strtotime($dt_time));
    $time = date('H:i:s',strtotime($dt_time));
    if(!strcmp($time, "15:00:00"))
    {
        /*

         now time is 15:00:00
         this is how to get temp_min and max at a particular time 
         you need to traverse json object 

         */

        $temp_min=$list[$i]['main']['temp_min'];
        $temp_max=$list[$i]['main']['temp_max'];
        echo '<br/><br/><h3>'.$date.' Status </h2><br/>';
        echo 'Temp_min: '.$temp_min;
        echo '<br/>Temp_max: '.$temp_max;
    }
}
?>

答案 1 :(得分:1)

    <?php
    // Decode string into JSON array
    $d = json_decode("{\"city\":{\"id\":1609350,\"name\":\"Bangkok\",\"coord\":{\"lon\":100.51667,\"lat\":13.75},\"country\":\"TH\",\"population\":0,\"sys\":{\"population\":0}},\"cod\":\"200\",\"message\":0.0169,\"cnt\":38,\"list\":[{\"dt\":1466402400,\"main\":{\"temp\":311.79,\"temp_min\":308.315,\"temp_max\":311.79,\"pressure\":1018.37,\"sea_level\":1018.77,\"grnd_level\":1018.37,\"humidity\":61,\"temp_kf\":3.47},\"weather\":[{\"id\":801,\"main\":\"Clouds\",\"description\":\"few clouds\",\"icon\":\"02d\"}],\"clouds\":{\"all\":24},\"wind\":{\"speed\":2.14,\"deg\":269.001},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-20 06:00:00\"},{\"dt\":1466413200,\"main\":{\"temp\":309.22,\"temp_min\":306.611,\"temp_max\":309.22,\"pressure\":1016.83,\"sea_level\":1017.28,\"grnd_level\":1016.83,\"humidity\":61,\"temp_kf\":2.6},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"clouds\":{\"all\":36},\"wind\":{\"speed\":1.07,\"deg\":132.503},\"rain\":{\"3h\":1.88},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-20 09:00:00\"},{\"dt\":1466424000,\"main\":{\"temp\":307.17,\"temp_min\":305.429,\"temp_max\":307.17,\"pressure\":1017.62,\"sea_level\":1018.09,\"grnd_level\":1017.62,\"humidity\":59,\"temp_kf\":1.74},\"weather\":[{\"id\":804,\"main\":\"Clouds\",\"description\":\"overcast clouds\",\"icon\":\"04n\"}],\"clouds\":{\"all\":92},\"wind\":{\"speed\":1.33,\"deg\":153.501},\"rain\":{},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-20 12:00:00\"},{\"dt\":1466434800,\"main\":{\"temp\":303.96,\"temp_min\":303.092,\"temp_max\":303.96,\"pressure\":1019.69,\"sea_level\":1020.02,\"grnd_level\":1019.69,\"humidity\":66,\"temp_kf\":0.87},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04n\"}],\"clouds\":{\"all\":76},\"wind\":{\"speed\":4.26,\"deg\":180.504},\"rain\":{},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-20 15:00:00\"},{\"dt\":1466445600,\"main\":{\"temp\":301.672,\"temp_min\":301.672,\"temp_max\":301.672,\"pressure\":1019.5,\"sea_level\":1019.9,\"grnd_level\":1019.5,\"humidity\":74,\"temp_kf\":0},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03n\"}],\"clouds\":{\"all\":48},\"wind\":{\"speed\":3.06,\"deg\":163.501},\"rain\":{},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-20 18:00:00\"},{\"dt\":1466456400,\"main\":{\"temp\":300.708,\"temp_min\":300.708,\"temp_max\":300.708,\"pressure\":1018.73,\"sea_level\":1019.13,\"grnd_level\":1018.73,\"humidity\":82,\"temp_kf\":0},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04n\"}],\"clouds\":{\"all\":68},\"wind\":{\"speed\":1.66,\"deg\":141.5},\"rain\":{},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-20 21:00:00\"},{\"dt\":1466467200,\"main\":{\"temp\":301.217,\"temp_min\":301.217,\"temp_max\":301.217,\"pressure\":1020.18,\"sea_level\":1020.61,\"grnd_level\":1020.18,\"humidity\":79,\"temp_kf\":0},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"clouds\":{\"all\":68},\"wind\":{\"speed\":1.61,\"deg\":91.0013},\"rain\":{},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-21 00:00:00\"},{\"dt\":1466478000,\"main\":{\"temp\":306.368,\"temp_min\":306.368,\"temp_max\":306.368,\"pressure\":1020.81,\"sea_level\":1021.3,\"grnd_level\":1020.81,\"humidity\":62,\"temp_kf\":0},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03d\"}],\"clouds\":{\"all\":36},\"wind\":{\"speed\":2.86,\"deg\":298.001},\"rain\":{},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-21 03:00:00\"},{\"dt\":1466488800,\"main\":{\"temp\":308.988,\"temp_min\":308.988,\"temp_max\":308.988,\"pressure\":1019.11,\"sea_level\":1019.63,\"grnd_level\":1019.11,\"humidity\":49,\"temp_kf\":0},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03d\"}],\"clouds\":{\"all\":44},\"wind\":{\"speed\":4.12,\"deg\":266.505},\"rain\":{},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-21 06:00:00\"},{\"dt\":1466499600,\"main\":{\"temp\":305.343,\"temp_min\":305.343,\"temp_max\":305.343,\"pressure\":1017.43,\"sea_level\":1017.76,\"grnd_level\":1017.43,\"humidity\":67,\"temp_kf\":0},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10d\"}],\"clouds\":{\"all\":36},\"wind\":{\"speed\":3.87,\"deg\":212.501},\"rain\":{\"3h\":1.415},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-21 09:00:00\"},{\"dt\":1466510400,\"main\":{\"temp\":304.423,\"temp_min\":304.423,\"temp_max\":304.423,\"pressure\":1018.4,\"sea_level\":1018.97,\"grnd_level\":1018.4,\"humidity\":63,\"temp_kf\":0},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"}],\"clouds\":{\"all\":92},\"wind\":{\"speed\":2.05,\"deg\":196.508},\"rain\":{\"3h\":1.075},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-21 12:00:00\"},{\"dt\":1466521200,\"main\":{\"temp\":303.07,\"temp_min\":303.07,\"temp_max\":303.07,\"pressure\":1020,\"sea_level\":1020.58,\"grnd_level\":1020,\"humidity\":67,\"temp_kf\":0},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04n\"}],\"clouds\":{\"all\":68},\"wind\":{\"speed\":2.56,\"deg\":178.501},\"rain\":{},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-21 15:00:00\"},{\"dt\":1466532000,\"main\":{\"temp\":300.893,\"temp_min\":300.893,\"temp_max\":300.893,\"pressure\":1020.32,\"sea_level\":1020.72,\"grnd_level\":1020.32,\"humidity\":80,\"temp_kf\":0},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"}],\"clouds\":{\"all\":56},\"wind\":{\"speed\":1.62,\"deg\":242.5},\"rain\":{\"3h\":1.735},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-21 18:00:00\"},{\"dt\":1466542800,\"main\":{\"temp\":300.177,\"temp_min\":300.177,\"temp_max\":300.177,\"pressure\":1019.56,\"sea_level\":1020.01,\"grnd_level\":1019.56,\"humidity\":79,\"temp_kf\":0},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04n\"}],\"clouds\":{\"all\":56},\"wind\":{\"speed\":1.11,\"deg\":237.002},\"rain\":{},\"sys\":{\"pod\":\"n\"},\"dt_txt\":\"2016-06-21 21:00:00\"},{\"dt\":1466553600,\"main\":{\"temp\":301.577,\"temp_min\":301.577,\"temp_max\":301.577,\"pressure\":1020.42,\"sea_level\":1020.8,\"grnd_level\":1020.42,\"humidity\":74,\"temp_kf\":0},\"weather\":[{\"id\":802,\"main\":\"Clouds\",\"description\":\"scattered clouds\",\"icon\":\"03d\"}],\"clouds\":{\"all\":48},\"wind\":{\"speed\":2.11,\"deg\":288.01},\"rain\":{},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2016-06-22 00:00:00\"}]}", true);


    foreach( $d["list"] as $i){
        // Check if this element of list contains "15:00:00"
        if(strpos($i["dt_txt"],"15:00:00") != false){
            echo $i["main"]["temp_min"];
            echo $i["main"]["temp_max"];
            echo "\n";
        }
    }
    ?>

答案 2 :(得分:0)

我想你想要这样的东西: -

<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$data = '{"city":{"id":1609350,"name":"Bangkok","coord":{"lon":100.51667,"lat":13.75},"country":"TH","population":0,"sys":{"population":0}},"cod":"200","message":0.0169,"cnt":38,"list":[{"dt":1466402400,"main":{"temp":311.79,"temp_min":308.315,"temp_max":311.79,"pressure":1018.37,"sea_level":1018.77,"grnd_level":1018.37,"humidity":61,"temp_kf":3.47},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":24},"wind":{"speed":2.14,"deg":269.001},"sys":{"pod":"d"},"dt_txt":"2016-06-20 06:00:00"},{"dt":1466413200,"main":{"temp":309.22,"temp_min":306.611,"temp_max":309.22,"pressure":1016.83,"sea_level":1017.28,"grnd_level":1016.83,"humidity":61,"temp_kf":2.6},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":36},"wind":{"speed":1.07,"deg":132.503},"rain":{"3h":1.88},"sys":{"pod":"d"},"dt_txt":"2016-06-20 09:00:00"},{"dt":1466424000,"main":{"temp":307.17,"temp_min":305.429,"temp_max":307.17,"pressure":1017.62,"sea_level":1018.09,"grnd_level":1017.62,"humidity":59,"temp_kf":1.74},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"clouds":{"all":92},"wind":{"speed":1.33,"deg":153.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 12:00:00"},{"dt":1466434800,"main":{"temp":303.96,"temp_min":303.092,"temp_max":303.96,"pressure":1019.69,"sea_level":1020.02,"grnd_level":1019.69,"humidity":66,"temp_kf":0.87},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":76},"wind":{"speed":4.26,"deg":180.504},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 15:00:00"},{"dt":1466445600,"main":{"temp":301.672,"temp_min":301.672,"temp_max":301.672,"pressure":1019.5,"sea_level":1019.9,"grnd_level":1019.5,"humidity":74,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":48},"wind":{"speed":3.06,"deg":163.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 18:00:00"},{"dt":1466456400,"main":{"temp":300.708,"temp_min":300.708,"temp_max":300.708,"pressure":1018.73,"sea_level":1019.13,"grnd_level":1018.73,"humidity":82,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":1.66,"deg":141.5},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-20 21:00:00"},{"dt":1466467200,"main":{"temp":301.217,"temp_min":301.217,"temp_max":301.217,"pressure":1020.18,"sea_level":1020.61,"grnd_level":1020.18,"humidity":79,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":68},"wind":{"speed":1.61,"deg":91.0013},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-21 00:00:00"},{"dt":1466478000,"main":{"temp":306.368,"temp_min":306.368,"temp_max":306.368,"pressure":1020.81,"sea_level":1021.3,"grnd_level":1020.81,"humidity":62,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":36},"wind":{"speed":2.86,"deg":298.001},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-21 03:00:00"},{"dt":1466488800,"main":{"temp":308.988,"temp_min":308.988,"temp_max":308.988,"pressure":1019.11,"sea_level":1019.63,"grnd_level":1019.11,"humidity":49,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":44},"wind":{"speed":4.12,"deg":266.505},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-21 06:00:00"},{"dt":1466499600,"main":{"temp":305.343,"temp_min":305.343,"temp_max":305.343,"pressure":1017.43,"sea_level":1017.76,"grnd_level":1017.43,"humidity":67,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":36},"wind":{"speed":3.87,"deg":212.501},"rain":{"3h":1.415},"sys":{"pod":"d"},"dt_txt":"2016-06-21 09:00:00"},{"dt":1466510400,"main":{"temp":304.423,"temp_min":304.423,"temp_max":304.423,"pressure":1018.4,"sea_level":1018.97,"grnd_level":1018.4,"humidity":63,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":92},"wind":{"speed":2.05,"deg":196.508},"rain":{"3h":1.075},"sys":{"pod":"n"},"dt_txt":"2016-06-21 12:00:00"},{"dt":1466521200,"main":{"temp":303.07,"temp_min":303.07,"temp_max":303.07,"pressure":1020,"sea_level":1020.58,"grnd_level":1020,"humidity":67,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":68},"wind":{"speed":2.56,"deg":178.501},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-21 15:00:00"},{"dt":1466532000,"main":{"temp":300.893,"temp_min":300.893,"temp_max":300.893,"pressure":1020.32,"sea_level":1020.72,"grnd_level":1020.32,"humidity":80,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":56},"wind":{"speed":1.62,"deg":242.5},"rain":{"3h":1.735},"sys":{"pod":"n"},"dt_txt":"2016-06-21 18:00:00"},{"dt":1466542800,"main":{"temp":300.177,"temp_min":300.177,"temp_max":300.177,"pressure":1019.56,"sea_level":1020.01,"grnd_level":1019.56,"humidity":79,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"clouds":{"all":56},"wind":{"speed":1.11,"deg":237.002},"rain":{},"sys":{"pod":"n"},"dt_txt":"2016-06-21 21:00:00"},{"dt":1466553600,"main":{"temp":301.577,"temp_min":301.577,"temp_max":301.577,"pressure":1020.42,"sea_level":1020.8,"grnd_level":1020.42,"humidity":74,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":2.11,"deg":288.01},"rain":{},"sys":{"pod":"d"},"dt_txt":"2016-06-22 00:00:00"}]}'; // your json encoded data

$json_array = json_decode($data); // decode json data
echo "<pre/>";print_r($json_array); // print original array
$final_array = array(); // take a empty array
foreach ($json_array->list as $json_arra){ // iterate loop
   if(strpos($json_arra->dt_txt,'15:00:00')){ // check if dt_txt contains 15:00:00 or not
       $final_array[$json_arra->dt_txt] = array('temp_min'=>$json_arra->main->temp_min,'temp_max'=>$json_arra->main->temp_max); // if yes then create a sub-array in the main empty array with corresponding date index
   }
}
echo "<pre/>";print_r($final_array); // print final array
?>

输出: - https://eval.in/591878