Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP按数组日期降序排序_Php - Fatal编程技术网

使用PHP按数组日期降序排序

使用PHP按数组日期降序排序,php,Php,我有这个阵列 Array ( [0] => Array ( [meeting_archive_id] => 1 [corp_aggregate_id] => 1 [created_by] => 103 [created_to] => 2 [date] =>

我有这个阵列

 Array
    (
        [0] => Array
            (
                [meeting_archive_id] => 1
                [corp_aggregate_id] => 1
                [created_by] => 103
                [created_to] => 2
                [date] => 15-07-2016
                [time] => 10:00 am
                [entry_id] => 103
                [user_type] => 1
                [firstname] => Kenyon
                [lastname] => Martena
                [job_title] => 
                [email] => 
                [company_name] => Lorem Ipsum Sodales Incorporated
                [phone] => 1-446-161-3194
                [type] => meeting
            )

        [1] => Array
            (
                [meeting_archive_id] => 16
                [corp_aggregate_id] => 2
                [created_by] => 103
                [created_to] => 6
                [date] => 17-07-2016
                [time] => 1:00 am
                [entry_id] => 103
                [user_type] => 1
                [firstname] => Hedley
                [lastname] => Aurelia
                [job_title] => 
                [email] => 
                [company_name] => Sit Amet Ante Corp.
                [phone] => 1-484-144-8520
                [type] => meeting
            )

        [2] => Array
            (
                [user_type] => 2
                [firstname] => Abbot
                [lastname] => Odessa
                [job_title] => 
                [email] => 
                [company_name] => Fermentum Vel Mauris Consulting
                [phone] => 1-912-440-1465
                [type] => event
                [meeting_archive_id] => 69
                [date] => 02-08-2016
                [time] => 8.00
                [corp_aggregate_id] => 
                [inves_aggregate_id] => 
            )

        [3] => Array
            (
                [user_type] => 1
                [firstname] => Kenyon
                [lastname] => Martena
                [job_title] => 
                [email] => 
                [company_name] => Lorem Ipsum Sodales Incorporated
                [phone] => 1-446-161-3194
                [type] => event
                [meeting_archive_id] => 70
                [date] => 15-07-2016
                [time] => 8.00
                [corp_aggregate_id] => 
                [inves_aggregate_id] => 
            )

    )
我需要按日期对这个数组排序


您可以尝试使用usort:

usort($array, function($a, $b) {
   $d1 = strtotime($a['date']);
   $d2 = strtotime($b['date']);
   return $d1 - $d2;
});
$a
$b
是属于主数组的数组
$array
,然后比较它们的时间

usort的原始定义是:

bool usort ( array &$array , callable $value_compare_func )
可调用函数为:

int callback ( mixed $a, mixed $b )
function($a, $b) {
   $d1 = strtotime($a['date']);
   $d2 = strtotime($b['date']);
   return $d1 - $d2;
}
在out情况下,可调用函数为:

int callback ( mixed $a, mixed $b )
function($a, $b) {
   $d1 = strtotime($a['date']);
   $d2 = strtotime($b['date']);
   return $d1 - $d2;
}
usort将数组作为第一个参数,第二个参数作为可调用函数。除了获取数组的两个元素之外,该函数是可调用的。在内部,它使用快速排序算法,可调用函数应返回整数值0、1或-1

如果两个日期(或任何应该比较的日期)相等,则函数应返回0

如果第一个元素较大,则应返回1 如果第二个元素较大,则应返回-1


输入数组(usort函数中的第一个参数)是通过引用传递的,因此在调用usort后,您将立即对数组进行排序。

您可以按如下方式尝试此代码段:

foreach($originalArray作为$key=>$part){
$sort[$key]=strottime($part['date']);
}

数组\u multisort($sort,sort\u DESC,$originalArray)

的可能重复项难道您不能在
选择
上对其进行排序吗?如果从db获取此数据,请使用“按日期排序”说明我正在将两个数组合并为一个数组,那么我只需要此类型的排序,在这种情况下,我可以将数组的
日期
时间
索引连接起来。