Update while loop with foreach