[英]PHP compare previous values and store latest value in array
Following is my code to bring all the latest dates in a month from dates array 以下是我的代码以将日期数组中一个月中的所有最新日期带入
$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);
Above code is printing values of first occurance date in a month. 上面的代码是一个月中第一次出现日期的打印值。 But i want the latest date from month. 但是我想要一个月的最新日期。
Above code gives following output 上面的代码给出以下输出
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 ) )
But expected code is 但是预期的代码是
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 ) )
Instead of printing first date in month, i want last date of month to be printed. 我希望打印月份的最后日期,而不是打印月份的第一个日期。 how can i get that?? 我怎么能得到呢?
Your code could be reduced a lot. 您的代码可以减少很多。 Here's what I could do: 这是我可以做的:
$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);
It's a simple logic that will check if the value is higher inside the same year/month and replace if it is. 这是一个简单的逻辑,它将检查同一年/月内该值是否较高,如果存在则替换该值。
Result: 结果:
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 ) ) )
Like this: 像这样:
foreach($dates as $d) {
list($y,$m) = explode("/",$d);
$years[$y][] = $d;
$months[$y."/".$m] = [$d];
}
Don't worry about keeping track of the current value. 不必担心跟踪当前值。 Just keep overwriting $months[$y."/".$m]
as you go, and you'll end up with the last date for each month. 只要继续覆盖$months[$y."/".$m]
,您就会得到每个月的最后一个日期。
If you want to get the most recent date from each month, rather than just the last one in the input array, make sure $dates
is sorted before you start the loop. 如果要获取每个月的最新日期,而不仅仅是输入数组中的最后一个$dates
,请确保在开始循环之前对$dates
进行了排序。 Since they're in year/month/day format, a simple sort($dates);
由于它们采用年/月/日的格式,因此很简单sort($dates);
should work. 应该管用。 (In the case of your example, $dates
is already sorted so this won't make any difference.) (在您的示例中, $dates
已经排序,因此不会有任何区别。)
This seems simplest to me: 这对我来说似乎最简单:
$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);
Output: 输出:
array (
0 => '2017/03/06',
1 => '2017/04/16',
2 => '2017/06/19',
3 => '2017/07/07',
4 => '2017/08/19',
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.