title: Transposing posted multiple arrays into codeigniter's insert_batch style
body: <p>I am trying to create multi-lingual form with Codeigniter.</p>
  
  <p>I have an POST array as:</p>
  
  <pre><code>Array
  (
    [title] =&gt; Array
      (
          [en] =&gt; English Title
          [de] =&gt; German Title
      )
  
    [url] =&gt; Array
      (
          [en] =&gt; English URL
          [de] =&gt; German URL
      )
  )
  </code></pre>
  
  <p>I want to convert this into codeigniter <code>insert_batch('tablename',$data)</code> format like:</p>
  
  <pre><code>Array
  (
    [0] =&gt; Array
      (
          [lang] =&gt; en
          [title] =&gt; English Title
          [url] =&gt; English URL
      )
  
    [1] =&gt; Array
      (
         [lang] =&gt; de  // Assuming user didn't fill `_de` labeled input fields.
         [title] =&gt; 
         [url] =&gt;
      )
  ) 
  </code></pre>
  
  <p>Data to be stored in db as:</p>
  
  <pre><code>|id|lang|    title      |    url      |
  |--|----|---------------|-------------|
  |  | en | English Title | English URL |
  ---------------------------------------
  </code></pre>
  
  <p>How can i achieve this? I need to transpose post array to <code>insert_batch</code>'s data array style. Above, <code>array[1]</code> contains only prefined <code>lang</code> as <code>de</code>. So <code>array[1]</code> is empty for me literally. So i also need to remove empty arrays from final <code>$data</code>. </p>
  
  <p>Below, you will find view file.</p>
  
  <pre><code>&lt;div id="tabs"&gt;
    &lt;?php echo form_open('controller/post_target',array('name'=&gt;'add_page')); ?&gt;
    &lt;ul&gt;
      &lt;?php foreach($lang_data as $r):?&gt;
      &lt;li&gt;&lt;a href="#tab_&lt;?=$r-&gt;lang;?&gt;"&gt;&lt;?=$r-&gt;name;?&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;?php endforeach; ?&gt;        
    &lt;/ul&gt;
  
    &lt;?php foreach($query_lang as $r):?&gt;
    &lt;div id="tab_&lt;?=$r-&gt;lang;?&gt;"&gt;
      &lt;?php echo form_label('Page Title','title'); ?&gt;
      &lt;?php 
        $attr1 = array('name' =&gt; 'title[' . $r-&gt;lang . ']');
        echo form_input($attr1); 
  
        $attr2 = array(name' =&gt; 'url[' . $r-&gt;lang . ']');
        echo form_input($attr2); 
      ?&gt;
    &lt;/div&gt;
    &lt;?php endforeach; ?&gt;
      &lt;?php echo form_submit('submit','Save'); ?&gt;
    &lt;?php echo form_close(); ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Just for info, I scratched a few line as controller but I want to have a clean-coded controller about this. Also my version can't check it if array empty. :( Any advise for more professional way is welcomed.</p>
  
  <pre><code>if($this-&gt;input-&gt;post('submit'))
  {
    $data = $this-&gt;input-&gt;post();
    unset($data['submit']); // SELF NOTE : IT should be easier way to do this   
  
    foreach ($data as $key =&gt; $value)
    {
      $counter = 0;
      foreach ($value as $key_inner =&gt; $value_inner)
      {
        if( ! isset($db_array[$counter])) $db_array[$counter] = array();
        //Above line enable us to use '+=' operand below in foreach loop
  
        $db_array[$counter] += array(
            'lang' =&gt; $key_inner,
            $key =&gt; $value_inner
        );          
        $counter ++;
      }
    }
  
    //echo '&lt;pre&gt;'; print_r($db_array); echo '&lt;/pre&gt;' . '&lt;br /&gt;';
  
    if($this-&gt;db-&gt;insert_batch('test',$db_array)) echo 'record saved';
  }
  </code></pre>