我正在为去年创建一个日期数组,并给出一个开始和结束日期,我将返回一组过滤日期。
我有一个问题,我不知道它是PHP版本之间的差异或Laravel踢了一个大惊小怪,但我得到的错误是
只应在第234行通过引用传递变量
是的
$result[array_pop(array_keys($result))][] = $val;
这是我的类函数,它给出了错误
public function filter(Request $request)
{
$time = new DateTime('now');
$now = $time->modify('first day of this month')->format('Y-m-d');
$last_year = $time->modify('-1 year')->format('Y-m-d');
// get a lost of dates fro past year
$all = $this->dateRange($last_year, $now);
foreach($request->dates as $date) {
// get date ranges of completed addresses
$range = $this->dateRange($date[0], $date[1]);
// return an array of unconfirmed dates for addresses
$all = array_diff($all, $range);
}
if(empty($all)) {
$time = new DateTime('now');
$now = $time->format('M Y');
$last_year = $time->modify('-1 year')->format('M Y');
$dates[] = array(
$last_year, $now
);
}
else {
$time = new DateTime('now');
$last_year = $time->modify('first day of this month')->modify('-1 year');
$result = array();
foreach ($all as $key => $val) {
if ($last_year->add(new DateInterval('P1D'))->format('Y-m-d') != $val) {
$result[] = array();
$last_year = new DateTime($val);
}
$result[array_pop(array_keys($result))][] = $val;
}
foreach($result as $array) {
$dates[] = array(
(new DateTime($array[0]))->format('M Y'), (new DateTime(end($array)))->format('M Y')
);
}
}
return response()->json($dates);
}
private function dateRange($start, $end)
{
$period = new DatePeriod(
new DateTime($start),
new DateInterval('P1D'),
new DateTime($end)
);
foreach($period as $key => $value) {
$range[] = $value->format('Y-m-d');
}
return $range;
}
这是在php7中运行,如果我使用php5.6在普通的php文件中运行代码,我没有得到任何错误,输出正是我所期望的。
是什么导致了问题以及如何解决?
你的问题与日期无关,但事实上array_pop
需要引用array_keys
不返回的数组。
你可以找到解释这个here的另一个答案
问题是,该端需要引用,因为它修改了数组的内部表示(即它使当前元素指针指向最后一个元素)。
爆炸('。',$ file_name)的结果无法转换为引用。这是PHP语言中的限制,可能出于简单原因而存在。
在你的情况下,array_keys
的结果是一个数组,而不是一个引用。
$result[array_pop(array_keys($result))][] = $val;
$poppedKey = array_pop(array_keys($result));
$result[$poppedKey][] = $val;
$keys = array_keys($result);
$poppedKey = array_pop($keys);
$result[$poppedKey][] = $val;
$keys = array_keys($result);
$result[array_pop($keys)][] = $val;