change while loop with foreach