W
W
WQP2015-01-06 16:01:02
PHP
WQP, 2015-01-06 16:01:02

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"
      }, {

How to sort by start (date format), no matter how much I searched, it doesn’t work ... Help someone not difficult

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cyapa, 2015-01-06
@WQP

$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');

A
Alexander Aksentiev, 2015-01-06
@Sanasol

to start with json_decode, and then everything is simple through usort

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question