Since each() is deprecated, I used key() to tell whether it had reached the end of the array. Although the documentation said it returned false upon reaching the end of the array, tests showed that it returned null. So, I just checked whether it was empty, which should be safe, as the key will have incremented beyond 0 after calling next(). So, now the foreach command will not terminate early when reaching a false value, and your original code should now work properly.
Since each() is deprecated, I used key() to tell whether it had reached the end of the array. Although the documentation said it returned false upon reaching the end of the array, tests showed that it returned null. So, I just checked whether it was empty, which should be safe, as the key will have incremented beyond 0 after calling next(). So, now the foreach command will not terminate early when reaching a false value, and your original code should now work properly.