在PHP中按开始/结束日期获取数组元素

时间:2016-01-05 10:27:12

标签: php arrays sorting

我想知道一个很好的方法来检查数组中的哪个元素是其开始/结束日期的前导元素(具有优先级)。

例如,这是数组:

(
    [0] => Array
    (
        [ID] => 16
        [begin_date] => DateTime Object
        (
            [date] => 2016-01-01
            [timezone_type] => 3
            [timezone] => UTC
        )
        [end_date] => DateTime Object
        (
            [date] => 2016-11-30
            [timezone_type] => 3
            [timezone] => UTC
        )
    )

    [1] => Array
    (  
        [ID] => 33
        [begin_date] => DateTime Object
        (
            [date] => 2015-01-04
            [timezone_type] => 3
            [timezone] => UTC
        )
        [end_date] => DateTime Object
        (
            [date] => 2016-02-29
            [timezone_type] => 3
            [timezone] => UTC
        )
    )
)

在这种情况下,第二个元素具有优先权。因为它的begin_date早于第一个元素的begin_date。并且,它的end_date仍然在未来。

条件:

  • 如果begin_date是将来的,则它不能是主要元素
  • 如果end_date已过期(如果今天&gt; end_date),则前导< element必须是具有最新end_date
  • 的元素

提前致谢!

1 个答案:

答案 0 :(得分:-1)

 uasort($memberArray,function($a,$b){
            $c = $a['begin_date']['date'] -  $b['begin_date']['date';
            $c .= $a['overall']['PD'] -  $b['overall']['PD'];
            $c .= $a['overall']['PF'] -  $b['overall']['PF'];
            $c .= $a['overall']['W'] -  $b['overall']['W'];
            $c .= $a['overall']['MP'] -  $b['overall']['MP'];
            return $c;
        });