如何在codeigniter中的foreach循环中设置验证规则

I need to set rules dynamically I don't know what will be the name of input type. For example I have input type<input type="text" name="first_name"> I can set rule for it like

$this->form_validation->set_rules("first_name", "First Name", "required");

But I am doing it dynamically suppose I have $_POST array I don't know the input type name value. So I did it this way

foreach($post as $key => val){

  $this->form_validation->set_rules($key, remove_underscore($key), "required");      

}

$this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");

    if($this->form_validation->run()){
      //do stuff here..
    }

The validation does not work in this loop way. If I set rules in a string and echo it outside the loop like

$str = '';
   foreach($post as $key => val){
     $str .= '$this->form_validation->set_rules($key'
   }

   echo $str;

That doesn't work either because I have to do echo json_encode(var) at the end. If I echo it first it will stop there only. I hope I have clear my point, if any one can guide me I will appreciate.