Change for loop with foreach