我需要按日期对多维数组进行排序的帮助

时间:2019-02-11 01:16:45

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

我无法按日期对多维数组进行排序。我希望$ list_array按'date'排序

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

1 个答案:

答案 0 :(得分:2)

只需使用array_multisort(请参见manual page的第三个示例),并将其作为对date字段进行排序的第一个数组(使用array_column进行提取)即可:< / p>

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

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

Demo on 3v4l.org