I am creating an array of dates for the last year and given a start and end date, i am returning an array of filtered dates.
I have a problem and I don't know if it's differences between php versions or Laravel kicking up a fuss but the error i'm getting is
Only variables should be passed by reference on line 234
which is
$result[array_pop(array_keys($result))][] = $val;
This is my class functions which give the error
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;
}
This is running in php7 where if I run the code in a plain php file using php5.6, I get no error and the output is exactly what I expect.
What is causing the problem and how to fix?