PHP比较以前的值并在数组

时间:2017-06-28 20:08:11

标签: php arrays

以下是我的代码,用于显示日期数组

中一个月内的所有最新日期
$dates = Array
(
 "2017/03/05",
 "2017/03/06",
 "2017/04/01",
 "2017/04/16",
 "2017/06/16",
 "2017/06/19",
 "2017/07/07",
 "2017/08/19",

);
$curr_val = '';
$years = Array();
$months = Array();
foreach($dates as $d) {
    list($y,$m) = explode("/",$d);
    $years[$y][] = $d;

if ($m != $curr_val){
       $months[$y."/".$m][] = $d;
      $curr_val = $m ;
   } 
}
$years = array_values($years);
$months = array_values($months);

print_r($months);

上面的代码是打印一个月内第一个出现日期的值。但我想要从一个月开始的最新日期。

上面的代码给出了以下输出

Array ( [0] => Array ( [0] => 2017/03/05 ) [1] => Array ( [0] => 2017/04/01 ) [2] => Array ( [0] => 2017/06/16 ) [3] => Array ( [0] => 2017/07/07 ) [4] => Array ( [0] => 2017/08/19 ) ) 

但预期的代码是

Array ( [0] => Array ( [0] => 2017/03/06 ) [1] => Array ( [0] => 2017/04/16 ) [2] => Array ( [0] => 2017/06/19 ) [3] => Array ( [0] => 2017/07/07 ) [4] => Array ( [0] => 2017/08/19 ) ) 

我希望打印月份的最后日期,而不是按月打印第一个日期。我该怎么办?

3 个答案:

答案 0 :(得分:1)

您的代码可能会减少很多。这就是我能做的:

$dates = Array
(
 "2017/03/05",
 "2017/03/06",
 "2017/04/01",
 "2017/04/16",
 "2017/06/16",
 "2017/06/19",
 "2017/07/07",
 "2017/08/19",

);
$array = [];
foreach($dates as $d) {
    $date = \DateTime::createFromFormat('Y/m/d' , $d);

    if (
        !isset($array[$date->format('Y')][$date->format('m')]) 
        || $array[$date->format('Y')][$date->format('m')] < $date
    ) {
        $array[$date->format('Y')][$date->format('m')] = $date;
    }         
}

print_r($array);

这是一个简单的逻辑,它将检查同一年/月内的值是否更高,如果是,则替换。

结果:

Array (
[2017] => Array
    (
        [03] => DateTime Object
            (
                [date] => 2017-03-06 22:18:42.000000
                [timezone_type] => 3
                [timezone] => Europe/Amsterdam
            )

        [04] => DateTime Object
            (
                [date] => 2017-04-16 22:18:42.000000
                [timezone_type] => 3
                [timezone] => Europe/Amsterdam
            )

        [06] => DateTime Object
            (
                [date] => 2017-06-19 22:18:42.000000
                [timezone_type] => 3
                [timezone] => Europe/Amsterdam
            )

        [07] => DateTime Object
            (
                [date] => 2017-07-07 22:18:42.000000
                [timezone_type] => 3
                [timezone] => Europe/Amsterdam
            )

        [08] => DateTime Object
            (
                [date] => 2017-08-19 22:18:42.000000
                [timezone_type] => 3
                [timezone] => Europe/Amsterdam
            )

    )

)

答案 1 :(得分:1)

像这样:

foreach($dates as $d) {
    list($y,$m) = explode("/",$d);
    $years[$y][] = $d;
    $months[$y."/".$m] = [$d];
}

不要担心跟踪当前值。只需继续覆盖$months[$y."/".$m],您就可以获得每个月的最后日期。

如果要获取每个月的最新日期,而不是输入数组中的最后一个日期,请确保在开始循环之前对$dates进行排序。由于它们采用年/月/日格式,因此简单的sort($dates);应该有效。 (在您的示例中,$dates已经排序,因此不会产生任何差异。)

答案 2 :(得分:0)

这对我来说似乎最简单:

代码:(Demo

$dates[  
    "2017/03/05",
    "2017/03/06",
    "2017/04/01",
    "2017/04/16",
    "2017/06/16",
    "2017/06/19",
    "2017/07/07",
    "2017/08/19"
];
foreach($dates as $d){
    $result[substr($d,0,7)]=$d;  // extract unique key from values
}
$result=array_values($result);  // reindex array
var_export($result);

输出:

array (
  0 => '2017/03/06',
  1 => '2017/04/16',
  2 => '2017/06/19',
  3 => '2017/07/07',
  4 => '2017/08/19',
)