简体   繁体   中英

Laravel comparing dates returns error on php7 but not on php5.6

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?

Your issue is nothing to do with dates, but is in fact that array_pop requires a reference to the array which array_keys does not return.

You can find another answer explaining this here which says

The problem is, that end requires a reference, because it modifies the internal representation of the array (ie it makes the current element pointer point to the last element).

The result of explode('.', $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.

In your case the result of array_keys is an array, not a reference.

  1. Fails

     $result[array_pop(array_keys($result))][] = $val; 
  2. Fails

     $poppedKey = array_pop(array_keys($result)); $result[$poppedKey][] = $val; 
  3. Works

     $keys = array_keys($result); $poppedKey = array_pop($keys); $result[$poppedKey][] = $val; 
  4. Works

     $keys = array_keys($result); $result[array_pop($keys)][] = $val; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM