Forgive me, I thought it deserved its own thread.
I did this myself, so let me know if I messed up somewhere:
Spain win, Portugal win: Both go through, whoever gets the biggest win (Goal Difference) or scores more if GD is same (Goals For) will finish first, otherwise fair play ranking (basically the number of yellow cards, second yellows and red cards) and then drawing of lots (random lottery) decides.
Spain win, Portugal/Iran draw: 1st - Spain, 2nd - Portugal
Spain win, Iran win: 1st - Spain, 2nd - Iran
Spain draw, Portugal win: 1st - Portugal, 2nd - Spain
Spain draw, Portugal/Iran draw: Spain and Portugal go through, whoever has the highest scoring draw finishes first, otherwise fair play and then drawing of lots decides.
Spain draw, Iran win: 1st - Iran, 2nd - Spain
Spain lose, Portugal win: 1st - Portugal, 2nd - Spain
Spain lose by 1 goal, Portugal/Iran draw: 1st - Portugal. Spain finish 2nd, unless Iran score 4 more goals in their draw (e.g. Spain 0 - 1 Morocco, Iran 4 - 4 Portugal)
Spain lose by 2 goals or more, Portugal/Iran draw: 1st - Portugal, 2nd - Iran
Spain lose, Iran win: 1st - Iran, 2nd - whoever loses by less (better goal difference) or scores the most in equal losses (goals scored) finishes 2nd (e.g. Spain finish 2nd if they lose 3-1 and Portugal lose 2-0). If both teams lose by the equal scoreline, fair play or drawing of lots decides.
I did this myself, so let me know if I messed up somewhere:
Spain win, Portugal win: Both go through, whoever gets the biggest win (Goal Difference) or scores more if GD is same (Goals For) will finish first, otherwise fair play ranking (basically the number of yellow cards, second yellows and red cards) and then drawing of lots (random lottery) decides.
Spain win, Portugal/Iran draw: 1st - Spain, 2nd - Portugal
Spain win, Iran win: 1st - Spain, 2nd - Iran
Spain draw, Portugal win: 1st - Portugal, 2nd - Spain
Spain draw, Portugal/Iran draw: Spain and Portugal go through, whoever has the highest scoring draw finishes first, otherwise fair play and then drawing of lots decides.
Spain draw, Iran win: 1st - Iran, 2nd - Spain
Spain lose, Portugal win: 1st - Portugal, 2nd - Spain
Spain lose by 1 goal, Portugal/Iran draw: 1st - Portugal. Spain finish 2nd, unless Iran score 4 more goals in their draw (e.g. Spain 0 - 1 Morocco, Iran 4 - 4 Portugal)
Spain lose by 2 goals or more, Portugal/Iran draw: 1st - Portugal, 2nd - Iran
Spain lose, Iran win: 1st - Iran, 2nd - whoever loses by less (better goal difference) or scores the most in equal losses (goals scored) finishes 2nd (e.g. Spain finish 2nd if they lose 3-1 and Portugal lose 2-0). If both teams lose by the equal scoreline, fair play or drawing of lots decides.