📜  在php代码示例中按日期元素对多维数组进行排序

📅  最后修改于: 2022-03-11 14:54:46.797000             🧑  作者: Mango

代码示例2
 "TEST_1",
            "datetime" => "2019-02-22 11:29:45",
        ),
        Array (
            "test" => "TEST_2",
            "datetime" => "2019-02-13 11:29:45",
        ),
        Array (
            "test" => "TEST_3",
            "datetime" => "2019-02-15 11:29:45",
        )
    );

    // Comparison function
    function dtCompare($ele1, $ele2) {
        $datetime1 = strtotime($ele1['datetime']);
        $datetime2 = strtotime($ele2['datetime']);
        return $datetime1 - $datetime2;
    }

    // Sort the array
    usort($array, 'dtCompare');

    // Print the array
    print_r($array)

?>

//Output

Array
(
    [0] => Array
        (
            [test] => TEST_2
            [datetime] => 2019-02-13 11:29:45
        )

    [1] => Array
        (
            [test] => TEST_3
            [datetime] => 2019-02-15 11:29:45
        )

    [2] => Array
        (
            [test] => TEST_1
            [datetime] => 2019-02-22 11:29:45
        )

)