Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sinres's avatar

OpenWeatherMap API - displaying data from the relevant array

Hello,

In my application for Weather I use OpenWeatherMap API. For Current Weather I don't have any problem but Forecast weather I choose array(8) and array(16) where I have weather forecast from 16:00.

{#224 ▼
  +"cod": "200"
  +"message": 0.04
  +"cnt": 22
  +"list": array:22 [▼
    0 => {#225 ▶}
    1 => {#232 ▶}
    2 => {#239 ▶}
    3 => {#246 ▶}
    4 => {#253 ▶}
    5 => {#260 ▶}
    6 => {#267 ▶}
    7 => {#274 ▶}
    8 => {#281 ▼
      +"dt": 1546614000
      +"main": {#282 ▶}
      +"weather": array:1 [▶]
      +"clouds": {#284 ▶}
      +"wind": {#285 ▶}
      +"snow": {#286 ▶}
      +"sys": {#287 ▶}
      +"dt_txt": "2019-01-04 15:00:00"
    }
    9 => {#288 ▶}
    10 => {#295 ▶}
    11 => {#302 ▶}
    12 => {#309 ▶}
    13 => {#316 ▶}
    14 => {#323 ▶}
    15 => {#330 ▶}
    16 => {#338 ▼
      +"dt": 1546700400
      +"main": {#339 ▶}
      +"weather": array:1 [▶]
      +"clouds": {#341 ▶}
      +"wind": {#342 ▶}
      +"rain": {#343 ▶}
      +"snow": {#344 ▶}
      +"sys": {#345 ▶}
      +"dt_txt": "2019-01-05 15:00:00"
    }
    17 => {#346 ▶}
    18 => {#354 ▶}
    19 => {#362 ▶}
    20 => {#370 ▶}
    21 => {#378 ▶}
  ]
  +"city": {#386 ▶}
}

The problem is that every 3 hours the data in the table changes and in the table (8) and (16) the weather forecast shows the forecast from a different time of day.

How can I filter the table to display data from subsequent days from 16:00?

Look this is my function in Interface for WeatherController

public function getForecastWeather()
    {
        return Cache::remember('forecast_weather', $minutes='60', function()
        {
            $jsonfile = file_get_contents("http://api.openweathermap.org/data/2.5/forecast?id=" . $this->city_id . "&cnt=22&appid=" . $this->api_key . "&units=" . $this->units . "");
            $jsondata_forecast = json_decode($jsonfile);

            return $jsondata_forecast;
        });
    }
0 likes
1 reply
Sinres's avatar

Any ideas? Usort function? But how display in two first array forecast weather from 3:00 PM from two days?

Please or to participate in this conversation.