Same variable used twice in foreach()