【问题标题】:I need help sorting a multi-dimensional array by date [duplicate]我需要帮助按日期对多维数组进行排序[重复]
【发布时间】:2019-07-04 11:45:29
【问题描述】:

我无法按日期对这个多维数组进行排序。我希望 $list_array 按“日期”排序

$list_array = array
    (
    array("date"=>"2020-01-02", "address"=>"1629 W Main St", "city"=>"Ville Platte", "client"=>"Anette Albert", "agent"=>"Michelle Rockwood", "office"=>"Gardner Realtors", "price"=>"445"),
    array("date"=>"2019-02-03", "address"=>"201 La Rue Rhiems", "city"=>"Duson", "client"=>"William Myers", "agent"=>"Teresa Pastor", "office"=>"District South", "price"=>"375"),
    array("date"=>"2019-01-07", "address"=>"1402 Southport Blvd", "city"=>"New Iberia", "client"=>"Jose Villatobos", "agent"=>"Rickey Romero", "office"=>"Keller Williams", "price"=>"550"),
    array("date"=>"2019-01-08", "address"=>"900 S. College Dr, Ste 100", "city"=>"Lafayette", "client"=>"Susan D'Picard", "agent"=>"John Allen", "office"=>"Keller Williams", "price"=>"325"),
    array("date"=>"2017-12-02", "address"=>"1629 W Main St", "city"=>"Ville Platte", "client"=>"Anette Albert", "agent"=>"Michelle Rockwood", "office"=>"Gardner Realtors", "price"=>"445"),
    array("date"=>"2016-02-03", "address"=>"202 La Rue Rhiems", "city"=>"Duson", "client"=>"Robert La'Rue", "agent"=>"Suzanne D'Ambrosio", "office"=>"Van Eaton", "price"=>"365"),
    array("date"=>"2019-01-07", "address"=>"1405 Southport Blvd", "city"=>"New Iberia", "client"=>"Bob Barker", "agent"=>"Rickey Romero", "office"=>"Keller Williams", "price"=>"560"),
    array("date"=>"2019-01-08", "address"=>"900 S. College Dr, Ste 103", "city"=>"Lafayette", "client"=>"Susan Johnson", "agent"=>"Fred Arse", "office"=>"Keller Williams", "price"=>"315")
    );

    usort($list_array, 'date_compare');

}

function date_compare($a, $b)
{
    $t1 = strtotime($a['date']);
    $t2 = strtotime($b['date']);
    return $t1 - $t2;
}  

我希望数组像下面的数组一样排序。

$list_array = array
    (
    array("date"=>"2016-02-03", "address"=>"202 La Rue Rhiems", "city"=>"Duson", "client"=>"Robert La'Rue", "agent"=>"Suzanne D'Ambrosio", "office"=>"Van Eaton", "price"=>"365"),
    array("date"=>"2017-12-02", "address"=>"1629 W Main St", "city"=>"Ville Platte", "client"=>"Anette Albert", "agent"=>"Michelle Rockwood", "office"=>"Gardner Realtors", "price"=>"445"),
    array("date"=>"2019-01-07", "address"=>"1405 Southport Blvd", "city"=>"New Iberia", "client"=>"Bob Barker", "agent"=>"Rickey Romero", "office"=>"Keller Williams", "price"=>"560"),
    array("date"=>"2019-01-07", "address"=>"1402 Southport Blvd", "city"=>"New Iberia", "client"=>"Jose Villatobos", "agent"=>"Rickey Romero", "office"=>"Keller Williams", "price"=>"550"),
    array("date"=>"2019-01-08", "address"=>"900 S. College Dr, Ste 100", "city"=>"Lafayette", "client"=>"Susan D'Picard", "agent"=>"John Allen", "office"=>"Keller Williams", "price"=>"325"),
    array("date"=>"2019-01-08", "address"=>"900 S. College Dr, Ste 103", "city"=>"Lafayette", "client"=>"Susan Johnson", "agent"=>"Fred Arse", "office"=>"Keller Williams", "price"=>"315"),
    array("date"=>"2019-02-03", "address"=>"201 La Rue Rhiems", "city"=>"Duson", "client"=>"William Myers", "agent"=>"Teresa Pastor", "office"=>"District South", "price"=>"375"),
    array("date"=>"2020-01-02", "address"=>"1629 W Main St", "city"=>"Ville Platte", "client"=>"Anette Albert", "agent"=>"Michelle Rockwood", "office"=>"Gardner Realtors", "price"=>"445")
    );

【问题讨论】:

    标签: php sorting date multidimensional-array associative-array


    【解决方案1】:

    只需使用array_multisort(参见manual page 上的第三个示例),将其作为第一个数组传递以对date 字段进行排序(使用array_column 提取):

    array_multisort(array_column($list_array, 'date'), $list_array);
    

    请注意,由于您的日期采用YYYY-MM-DD 形式,因此您无需对其进行转换即可对其进行排序。

    Demo on 3v4l.org

    【讨论】:

    • 完美运行!你为我节省了很多时间。非常感谢!
    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 2016-10-31
    • 2013-04-01
    • 2015-06-28
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多