Revert change to foreach order and use an isset() call instead.