0

I have this code.

foreach ($array as &$row) {
    // Do stuff to array
}

A var_dump afterwards shows the last index (107 in this case) of the array has a reference &.

[107]=>
&array(4) {
  ["foo"]=>
  string(2) "bar"
}

My problem is that a follow up foreach doesn't show the last index of the array, but instead repeats the second to last index.

foreach ($array as $row) {
    var_dump($row);
    // When it gets to 107, it'll just show 106 again
}

Why do I see this behavior and how can I ensure that the second foreach includes the last element of the array?

Goose
  • 4,764
  • 5
  • 45
  • 84
  • take a look here https://stackoverflow.com/questions/3307409/php-pass-by-reference-in-foreach – Avihay m Jun 12 '17 at 14:27
  • @Avihaym Yep that's exactly my problem. Couldn't find it in my searching. This question can be closed as dupe. – Goose Jun 12 '17 at 14:29

0 Answers0