Especially note the last two sentences.Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array.
In php 5.0.3 when the following code is executed
Code: Select all
$a = range('a', 'd');
echo 'key before: '.key($a).'<br \>';
foreach ($a as $idx => $value) {
echo 'key='.key($a)." idx=$idx (value=$value)<br \>";
}
echo 'key after: '.key($a).'<br \>';Code: Select all
key before: 0
key=0 idx=0 (value=a)
key=0 idx=1 (value=b)
key=0 idx=2 (value=c)
key=0 idx=3 (value=d)
key after: 0