0

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?

Peter Griffin
  • 300
  • 5
  • 18
  • i don't know what u are trying to do here but , it's preferably to use Carbon library [link](http://carbon.nesbot.com/docs/) – Thamer Jan 04 '18 at 10:45
  • @Thamerbelfkih This error doesn't have anything to do with DateTime – Jim Wright Jan 04 '18 at 10:49
  • @JimWright i said Carbon, maybe he find something can help his to figure out what he want to do , Carbon it's extension of DateTime php Class – Thamer Jan 04 '18 at 10:57
  • @Thamerbelfkih I know, but this error has nothing to do with dates and have already posted an answer. – Jim Wright Jan 04 '18 at 10:59

1 Answers1

1

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 (i.e. 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;
    
Jim Wright
  • 5,905
  • 1
  • 15
  • 34