PHP code inside shortcodes

You can not put PHP functions inside your shortcode.

However, you do not need to. You can just use your foreach-loop to create the html and store it in a variable, for example $shortcodehtml.

So, afterwards, you can call

echo do_shortcode('[O_U user_name="operator" blocked_message="This page is restricted for guests."]' . $shortcodehtml . '[/O_U]');

The better version of that would be if you would create the output inside the shortcode function – from my point of view it is not necessary to pass the whole html to the shortcode – but this should work fine.