Codeigniter 插入批处理数组

alw*_*ner 0 php arrays codeigniter

我正在使用 Codeigniter 将多表单输入数据插入到数据库中。我有这个帖子输入数组:

 Array
(
 [subject_id] => Array
    (
        [0] => 1
        [1] => 1
    )

[question] => Array

    (
        [0] => test
        [1] => test2
    )

[option1] => Array
    (
        [0] => test
        [1] => test2
    ) )
Run Code Online (Sandbox Code Playgroud)

我不明白如何转换此数组以插入如何使用插入批处理插入此数组。

$this->db->insert_batch('mytable', $data);
Run Code Online (Sandbox Code Playgroud)

这是我用于发布数据的表单代码:

<form method="post">
    <input type="text" name="subject_id[]" >
    <input type="text" name="question[]" >
    <input type="text" name="record[]" >

    // Down side Part is appended when user want to add more question

    <input type="text" name="subject_id[]" >
    <input type="text" name="question[]" >
    <input type="text" name="record[]" >

    <input type="submit" name="submit" >
</form>
Run Code Online (Sandbox Code Playgroud)

下面是我想要的数组格式。

$data = array(
    array(
        'subject_id' => 'My title' ,
        'question' => 'My Name' ,
        'option1' => 'My date'
        ),
    array(
        'subject_id' => 'Another title' ,
        'question' => 'Another Name' ,
        'option1' => 'Another date'
        )
    );
Run Code Online (Sandbox Code Playgroud)

dhr*_*dia 5

<?php
    $i = 0;
    foreach($subject_id as $key=>$val)
    {
          $data[$i]['subject_id'] = $val;
          $data[$i]['question'] = $question[$key];
          $data[$i]['option1'] = $record[$key];
          $i++;
    }
    $this->db->insert_batch('mytable', $data);

?>
Run Code Online (Sandbox Code Playgroud)