title: How can I process input when user can dynamically add sets of text fields?
body: <h3>HTML</h3>
  
  <p>In this the user can add up to 10 pairs of textfields on runtime; I'm using jQuery here.</p>
  
  <pre><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      $(document).ready(function(){
          var max_fields      = 10;
          var wrapper         = $(".container1");
          var add_button      = $(".add_form_field");
  
          var x=1;
          $(add_button).click(function(e){
              e.preventDefault();
              if(x &lt; max_fields){
  
                  x++;
                  $(wrapper).append('&lt;div&gt;&lt;input type="text" name="mytext[]"/&gt;&lt;/div&gt;&lt;div&gt;&lt;input type="text" name="mytext2[]"/&gt;&lt;a href="#" class="delete"&gt;Delete&lt;/a&gt;&lt;/div&gt;'); //add input box
              }
              else{
                  alert('You Reached the limits');
              }
          });
  
          $(wrapper).on("click", ".delete", function(e){
              e.preventDefault(); $(this).parent('div').remove(); x--;
          });
      });
  &lt;/script&gt;
  
  &lt;form action="&lt;?php base_url();?&gt;index.php/Welcome/formdata" method="POST"&gt;
  &lt;div class="container1"&gt;
      &lt;button class="add_form_field"&gt;Add New Field &amp;nbsp; &lt;span style="font-size:16px; font-weight:bold;"&gt;+ &lt;/span&gt;&lt;/button&gt;
      &lt;div&gt;&lt;input type="text" name="mytext[]"&gt;&lt;/div&gt;
      &lt;div&gt;&lt;input type="text" name="mytext2[]"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;input type="submit" name="submit"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <h3>PHP</h3>
  
  <p>Here i am getting all the info the user entered. Because every time the number of input fields change I tried to use a <code>foreach</code> loop to to enter one group of data at a time.
  I am using CodeIgniter to insert the data into the database. I dont want to use a <code>for</code> loop in my model - that's why I need <code>$data</code> to be an array similar to this</p>
  
  <pre><code>$data = array(
      array(
          'mytext1' =&gt; 'xx', 
          'mytext2' =&gt; 'xxx'),
      ),
      array(
          'mytext1' =&gt; 'xx', 
          'mytext2' =&gt; 'xxx'),
      )
  );
  </code></pre>
  
  <p>so that I can use <code>insert_batch</code>.</p>
  
  <p>But <code>array_merge()</code> is giving me only first data after loop ended.</p>
  
  <pre><code>&lt;?php
  
  defined('BASEPATH') OR exit('No direct script access allowed');
  
  class Welcome extends CI_Controller 
  {
      public function index()
      {
          $this-&gt;load-&gt;view('welcome_message');
      }
  
      public function formdata()
      {
          $data = array();
  
          foreach($_POST as $post) {
              $data2['mytext'] = $post[0];
              $data2['mytext2'] = $post[1];
  
              $data = array_merge($data2, $data);
          }
  
          print_r($data);
          exit;
      }
  }
  </code></pre>
  
  <p>It's not merging all the items its just give me the first input I enter in the form.</p>
  
  <p>How can I fix this?</p>