Please to remove useless blank line here.
But you can add one between each foreach and if to aerate this part of code.