0

How do you remove this warning?

Strict Standards: Only variables should be passed by reference in

I have code that gets unique values from a multidimensional array and then gets the last index.

$catchColors[]= array();

for ($i = 0; $i < $totalRows; $i++) {
    $catchColors[$i] = $postData[$i]['ColorID'];
}

$result = array_unique($catchColors);
print_r($result);

print end(array_keys($result));

it still returns the value, but how do I remove the warning?

Sikret Miseon
  • 557
  • 1
  • 11
  • 19
  • 3
    shouldn't it be `$catchColors = array();` ? – tereško Oct 08 '11 at 09:33
  • possible duplicate of [Strict Standards: Only variables should be passed by reference](http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) – Lorenz Meyer Jun 21 '14 at 08:40

1 Answers1

6

That's because the parameter for the function end will be passed by reference. Thus, it can't be a return of another function, it should be, as the notice says, an actual variable.

So, a solution is to create a temporary variable to hold the array of the keys, and then execute end in that array.

$keys = array_keys($result);
print end($keys);
Shef
  • 44,808
  • 15
  • 79
  • 90
  • Agreed. So why doesn't: "print end(($keys = array_keys($result)));" avoid the warning? Isn't the first parameter now a variable? – flymike Mar 31 '21 at 20:39