PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Well, it does explain it, but not sufficiently
I haven't made any assignments to the $arr after running the foreach code. I haven't done anything at all. Why does a simple loop over the array elements assign to the last value its previous one? Is this the normal behaviour?
I'm trying to avoid long verbal explanations because I'll only make it sound more complicated than it is. Better to edge towards an understanding by playing with code examples. For a start, do you entirely understand what's going on below, and when you execute it, do you get the result you expected? It's nearly the same thing as your code example, but the foreach's have been temporarily removed:
Each iteration of a foreach loop involves the assignment of the loop variable followed by the execution of the loop body, which is empty in your example, so your foreach loops are actually just sequences of assignments. So below is your example with the loops decomposed into sequences of assignments. There's no extra tricks in this example than in the previous example, there are just a few more assignments to keep track of.
Now I think I'm starting to get it. So, when the first loop finishes, $value points to the same place as $arr[1].
When the second loop starts, $value becomes $arr[0], but since $value is a reference to $arr[1], $arr[1] also becomes $arr[0].
Thus in the end, you get the last value being the same as the first previous one
Thank your really very much, this explanation really helped me!