change for loop with foreach to work directly with array keys