Using foreach loop breaks

I believe your issue is with $vals=get_post_meta($post_id, $key2, true);

If you check the codex, the last parameter for get_post_meta is whether to return a single value or array of values. You have set it to true which means return only one value.

Try it with false (the default) it should work.