根据值对数组进行排序

时间:2014-12-05 11:32:18

标签: php arrays

我试图找出如何对下面的数组进行排序,以便past数组项以start_date降序发送到数组的末尾。

修改即可。 Id可能在所有数组中包含一个时间数组键值项,以按start_date排序。

[216] => Array (
    [title] => Production 1
    [start_date] => 20th Feb
    [end_date] => 23rd Feb 2015
    [ticket_link] => http://www.google.co.uk
    [writer] => Sarah Ruhl
    [thumb_image] => /files/3514/1762/4350/Biz-Bio-Pic.jpg
    [past] => 1
)

[218] => Array(
    [title] => Production 3
    [start_date] => 27th Feb
    [end_date] => 2nd Mar 2015
    [ticket_link] => www.google.co.uk
    [writer] => Sarah Ruhl
    [thumb_image] => /files/9414/1762/4351/Dan-Bio-Pic.jpg
    [past] => 1
)

[219] => Array (
    [title] => Production 4
    [start_date] => 3rd Mar
    [end_date] => 5th Mar 2015
    [ticket_link] => www.google.co.uk
    [writer] => Sarah Ruhl
    [thumb_image] => /files/4314/1762/4351/Kate-Bio-Pic.jpg
    [past] => 0
)

1 个答案:

答案 0 :(得分:2)

试试这个 -

function checkdate($a, $b)
{
    $a = strtotime($a['start_date']);
    $b = strtotime($b['start_date']);

    if ($a == $b) {
        return 0;
    }

    return ($a > $b) ? -1 : 1;
}

function checkpast($a, $b)
{
    $a_start = strtotime($a['start_date']);
    $b_start = strtotime($b['start_date']);

    if ($a_start == b_start ) {
        return ($a['past'] > $b['past']) ? -1 : 1;
    }

}

$array = //your array

usort($array, "checkdate");
usort($array, "checkpast");