Supposed you have an indexed array in PHP…
$foo = array(‘apple’, ‘orange’, ‘banana’);
And you run…
foreach ($foo as $index => $bar) {
if ($index == 1) unset($foo[$index]);
then the resulting array will have these indexes!:
0, 2
If you then try to iterate the array in the standard fashion:
for($i = 0; $i < count($foo); $i++) {
you’ll get an error when $i is 1.
The fix for resetting indexes if you ever unset in a loop is to call:
$foo = array_values($foo);
after the loop!
Lesson: EVERY array in PHP is an associative array, even “indexed” arrays.