I have an array of this type:

Array ( [15709] => stdClass Object ( [pid] => 15709 [channel_ID] => 51 [date] => 2016-03-21 00:30:00 [program_info] => Х/ф "Стелла" ) [15710] => stdClass Object ( [pid] => 15710 [channel_ID] => 51 [date] => 2016-03-21 02:20:00 [program_info] => Х/ф "Часы доблести" ) [15711] => stdClass Object ( [pid] => 15711 [channel_ID] => 51 [date] => 2016-03-21 06:15:00 [program_info] => Фильм-концерт "Хичкок. Концерт в магазине" ) ) 

I need to sort it by the [date] field so that all the elements whose field [date] greater than 05:00:00 would go first, that is, the array looked like this:

 Array ( [15711] => stdClass Object ( [pid] => 15711 [channel_ID] => 51 [date] => 2016-03-21 06:15:00 [program_info] => Фильм-концерт "Хичкок. Концерт в магазине" ) [15709] => stdClass Object ( [pid] => 15709 [channel_ID] => 51 [date] => 2016-03-21 00:30:00 [program_info] => Х/ф "Стелла") [15710] => stdClass Object ( [pid] => 15710 [channel_ID] => 51 [date] => 2016-03-21 02:20:00 [program_info] => Х/ф "Часы доблести" ) ) 
  • > to first go all the elements whose [date] field is greater than 05:00:00. What do you mean by more than 5 in the morning? If you are not satisfied with descending, please specify your question. Because it may be different days '2016-03-20 06:00:00', '2016-03-21 02:00:00', what should be after sorting? - jekaby
  • @jekaby in my case, the days are the same - Artyom Kondra

1 answer 1

 // $arr - ваш массив usort($arr, function ($arr1, $arr2) { return $arr1['date'] < $arr2['date']; }); var_dump($arr); 

sorts descending 'date'. If everywhere in the 'date' timestamp, then the string comparison will work ok.

UPD : if you want to save the keys of the original array, then replace the usort function with uasort. manual .

  • according to the structure array('05:00','06:00','07:00','12:00','14:00','16:00','22:00','23:00','02:00','‌​04:00') - Artyom Kondra