Replaced foreach() + key() with for($i = 0...

Not sure why key() worked in the first place, it definitely doesn't
generally, and the code was flakey.