Code for a Foreach with Keys & Nested loops