Answer the question
In order to leave comments, you need to log in
How to sort a JSON array in PHP?
Hello, I have an array
"channel_type": {
"1": [{
"position": "95",
"name": "Первый",
"schedule": [{
"country": "Россия",
"stop": "2015-01-06 10:55:00",
"name": "Смак",
"genre_id": "140",
"episode_title": "Новогодние выпуски",
"id": "31780573",
"company": "\"Наш взгляд\"",
"year": "2014",
"start": "2015-01-06 10:15:00",
"episode_num": "58"
},
....
{
"country": "Россия",
"stop": "2015-01-06 08:00:00",
"name": "Красавчик",
"genre_id": "138",
"id": "31780590",
"company": "Кинокомпания \"Магнум\", ДТ Продакшн",
"start": "2015-01-06 06:20:00",
"year": "2011"
}, {
Answer the question
In order to leave comments, you need to log in
$json = 'ваш json';
$array = json_decode($json);
$array = $array['channel_type']['1']['schedule'];
function comparator($one, $two)
{
$one_time = strtotime($one['start']);
$two_time = strtotime($two['start']);
if($one_time == $two_time)
{
return 0;
}
return ($one_time < $two_time) ? -1 : 1;
}
uasort($array, 'comparator');
to start with json_decode, and then everything is simple through usort
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question