replace for loop with foreach