You cannot change PHP's core behaviour using an extension. If you want to change the foreach
loop you need to change core components of PHP and recompile it. It's up to you to find out what you'll have to change. (That's the fun)
However you may just doing this:
foreach (array_keys($myArray) as $key) {
// Do stuff here
}