foreach iterates over the array and assigns the key $ i and the value $ a before accessing the code block inside the loop. The array is actually "copied" by the function before repeating, so any changes to the original array do not affect the progression of the loop.
You can also pass the $ array by reference in foreach, using $i => &$a instead of a value that allows mutation of the array.
Another option is to work directly with the array, and you will see something else:
for($x=0;$x<count($array);$x++){ unset($array[1]); // for $x=1 this would result in an error as key does not exist now echo $array[$x]; } print_r($array);
Of course, this assumes that your array is numerically and sequentially entered.
source share