How to properly add multiple form elements in a loop?
Could you do something like the following:

$form['openingdiv'] = array(
  '#markup' =&gt; '&lt;div&gt;'
);

//Do all your foreach stuff here

$form['closingdiv'] = array(
  '#markup' =&gt; '&lt;/div&gt;'
);


(Sorry it's a bit simple..I'm tired :-p)