PHP排序数组,日期为键,日期格式 [英] php sort array with date as key with date format

查看:182
本文介绍了PHP排序数组,日期为键,日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组如下:
第一个关键元素是日期,格式为dd-mm-yyyy

I have an array as follow: The first key element is a date with the format dd-mm-yyyy

Array
(
    [08-12-2015] => Array
        (
          ------------
        )
    [07-12-2015] => Array
        (
          ------------
        )
    [09-12-2015] => Array
        (
          ------------
        )
)

是否可以在第一个键值bij日期对该数组进行排序?
因此,第一个元素是

Is it possible to sort this array on the first key value bij date? So the first element is

Array
(
    [07-12-2015] => Array
        (
          ------------
        )
    [08-12-2015] => Array
        (
          ------------
        )
    [09-12-2015] => Array
        (
          ------------
        )
)


推荐答案

是,我找到了解决方法:

Yes, I found the solution:

function order_date($a1,$b1) {
    $format = 'd-m-Y';
    $a = strtotime(date_format(DateTime::createFromFormat($format, $a1), 'Y-m-d H:i:s'));
    $b = strtotime(date_format(DateTime::createFromFormat($format, $b1), 'Y-m-d H:i:s'));
    if ($a == $b)
    {
        return 0;
    }
    else if ($a > $b)
    {
        return 1;
    }
    else {
        return -1;
    }
}

uksort($array, "order_date");

Tx。

这篇关于PHP排序数组,日期为键,日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆