Php 我需要帮助按日期排序多维数组
我无法按日期对多维数组进行排序。我希望$list\u数组按“日期”排序Php 我需要帮助按日期排序多维数组,php,sorting,date,multidimensional-array,associative-array,Php,Sorting,Date,Multidimensional Array,Associative Array,我无法按日期对多维数组进行排序。我希望$list\u数组按“日期”排序 $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"
$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")
);
只需使用
array\u multisort
(参见上的第三个示例),将其作为第一个数组传递,以对日期
字段进行排序(使用以下方法提取):
请注意,由于您的日期为YYYY-MM-DD
格式,因此不需要将其转换为排序
只需使用
array\u multisort
(参见上的第三个示例),将其作为第一个数组传递,以对日期
字段进行排序(使用以下方法提取):
请注意,由于您的日期为YYYY-MM-DD
格式,因此不需要将其转换为排序
工作完美!你为我节省了很多时间。非常感谢你!工作完美!你为我节省了很多时间。非常感谢你!
array_multisort(array_column($list_array, 'date'), $list_array);