Key element cannot be a reference

Description

In a foreach() loop, the value may be a reference, when it needs to be updated. The key, on the other hand, cannot be changed, and hence, cannot have a reference.

Example

<?php

foreach($array as &$key => $value) {}

?>

Solutions

  • Remove the reference from the key.

  • Use array_flip() to make the keys the values, and update them, then use the same function again.