There is an array (one-dimensional) with data about the weather forecast, it is necessary to pack up the data by day (today, tomorrow, the day after tomorrow). Array example:

31.03.2016 18:03 0.28 легкий дождь 31.03.2016 21:03 -1.75 легкий дождь 01.04.2016 00:04 -2.65 небольшой снегопад 01.04.2016 03:04 -3.42 снегопад 01.04.2016 06:04 -5.13 снегопад 01.04.2016 09:04 -5.34 небольшой снегопад 01.04.2016 12:04 -3.99 ясно 01.04.2016 15:04 -2.98 ясно 01.04.2016 18:04 -3.97 облачно 01.04.2016 21:04 -9.94 ясно 02.04.2016 00:04 -11.92 ясно 02.04.2016 03:04 -13.37 облачно 02.04.2016 06:04 -11.52 пасмурно 02.04.2016 09:04 -6.17 пасмурно 02.04.2016 12:04 -3.55 небольшой снегопад 02.04.2016 15:04 -3.71 небольшой снегопад 02.04.2016 18:04 -4.69 небольшой снегопад 02.04.2016 21:04 -5.73 небольшой снегопад 

Array structure:

  Array ( [0] => 31.03.2016 [1] => 18:03 [2] => 0.28 [3] => легкий дождь ) 

in php got variables with the date today, tomorrow the day after tomorrow:

 //ld - local_date $ld_today = date("dmY"); $ld_tomorrow = date("dmY", strtotime('tomorrow')); $ld_aftertomorrow = date("dmY", strtotime('tomorrow+1day')); echo "сегодня: ".$ld_today."\n"; echo "завтра: ".$ld_tomorrow."\n"; echo "послезавтра: ".$ld_aftertomorrow."\n"; 

Conclusion:

 сегодня: 31.03.2016 завтра: 01.04.2016 послезавтра: 02.04.2016 

How to package (make a sample of the array by day) data from this array by day, so that it is:

 сегодня 31.03.2016: 31.03.2016 18:03 0.28 легкий дождь 31.03.2016 21:03 -1.75 легкий дождь Завтра 01.04.2016: 01.04.2016 00:04 -2.65 небольшой снегопад 01.04.2016 03:04 -3.42 снегопад 01.04.2016 06:04 -5.13 снегопад 01.04.2016 09:04 -5.34 небольшой снегопад 01.04.2016 12:04 -3.99 ясно 01.04.2016 15:04 -2.98 ясно 01.04.2016 18:04 -3.97 облачно 01.04.2016 21:04 -9.94 ясно Послезавтра 02.04.2016: 02.04.2016 00:04 -11.92 ясно 02.04.2016 03:04 -13.37 облачно 02.04.2016 06:04 -11.52 пасмурно 02.04.2016 09:04 -6.17 пасмурно 02.04.2016 12:04 -3.55 небольшой снегопад 02.04.2016 15:04 -3.71 небольшой снегопад 02.04.2016 18:04 -4.69 небольшой снегопад 02.04.2016 21:04 -5.73 небольшой снегопад 
  • And 31.03.2016 18:03 0.28 легкий дождь and so on are presented as a string? Or is it all one big line? ... between the date and time gap? - Alexey Shimansky
  • date and time one variable (type date, you can even unix timestamp, whatever you like) just separated for display, and light rain - line - mocart
  • Apply to the source array usort php.net/manual/ru/function.usort.php and then in the loop just output - Alexey Shimansky

1 answer 1

If you do not use any templates and html, and in raw form display the data as they are, then, in general, it turns out like this:

At the beginning, we use the usort() function to the original array http://php.net/manual/ru/function.usort.php

usort — Sorts an array by value using a user-defined function to compare elements.

And after that, we output the data in a cycle using our own additional conditions (we write the day after tomorrow / tomorrow, etc.)

 $arr = [ ['07.04.2016', '18:03', '0.281', 'легкий дождь'], ['07.04.2016', '19:03', '0.282', 'смерч'], ['05.04.2016', '17:03', '0.183', 'лунное расщипление'], ['05.04.2016', '18:03', '0.184', 'взрыв солнца'], ['31.03.2016', '21:03', '0.185', '100 километровая комета'], ['31.03.2016', '01:03', '0.186', 'прилет НЛО'], ]; function strToTimeFunc($arr) { return strtotime($arr[0].' '.$arr[1]); } usort($arr, function($arr1, $arr2){ $time1 = strToTimeFunc($arr1); $time2 = strToTimeFunc($arr2); if ($time1 == $time2) { return 0; } return ($time1 < $time2) ? -1 : 1; }); // вывод данных $startTimeVal = date('dmY', strToTimeFunc($arr[0])); echo $startTimeVal.'<br />'; foreach ($arr as $test) { $time = date('dmY', strToTimeFunc($test)); if ($startTimeVal != $time) { echo '<br />'.$time.'<br />'; $startTimeVal = $time; } echo $test[0].' '.$test[1].' '.$test[2].' '.$test[3].'<br />'; } 

Taking into account the fact that the author has the date and time in different cells, he made an example based on this data. Of course, to simplify life, it is better that the date is found in one cell.

Ps if you have PHP7 , then usort can write usort in general like this:

 usort($arr, function($arr1, $arr2){ return strToTimeFunc($arr1) <=> strToTimeFunc($arr2); });