79713335

Date: 2025-07-24 12:22:19
Score: 3
Natty:
Report link

Even though the question is old AF, if anyone is still having the same issue (and even though the given answers provide a workaround for said issue) the answer to the question what the problem is, is the following:

When passing values by reference to a foreach it is important to unset the reference after the foreach is done as the reference to the last item of the array is still existing, even after the loop is done.
In that case the value behind that remaining reference is updated with all values of the iterable that is provided ($items in this case) until the last value of the array includes the second to last value.

This is why in the given scenario a "duplicate" is shown, when having 2 items and the reason why only the first item and twice the second item is shown with 3 items.

The behavior is known and documented in the PHP docs

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Me too answer (2.5): having the same issue
  • Low reputation (0.5):
Posted by: Mr.Moe