0

I'm trying to insert array data using insert_batch in active record as shown in below: Any ideas to prepare the array to insert using insert_batch or any other way?

  $detailBill=array(
        'TraineeID'=>$inputall['ID'],
        'wDate'=>$inputall['wDate'],
        'ACH'=>$inputall['Hour'],
        'CRA'=>$inputall['Retention'],
        'Amount'=>$inputall['PaybleAmt'],
        'forMonth'=>$inputall['monthid']
    );

$this->db->insert_batch("tbl_submit_coursefee", $detailBill);

Output:

Array
(
    [TraineeID]=> Array
    (
      [0]=>3001
      [1]=>3002
      [2]=>3003
      [3]=>3004
      [4]=>3005
      [5]=>3006
      [6]=>3007
      [7]=>3008
      [8]=>3009
      [9]=>3010
      [10]=>3011
      [11]=>3012
      [12]=>3013
    )
   [wDate]=> Array
   (
      [0]=>
      [1]=>
      [2]=>
      [3]=>
      [4]=>
      [5]=>
      [6]=>
      [7]=>
      [8]=>
      [9]=>
      [10]=>
      [11]=>
      [12]=>
   )
   more field here....
)
1
  • It's a bit confusing what you are doing right now, check again at the manual at codeigniter.com/user_guide/database/…. It explains pretty well how the structure of your array should look like Commented Mar 26, 2016 at 18:20

1 Answer 1

1

I am assuming you want to reorganize arrays. Try this:

$a = array(
    'TraineeID' => array(
        3001,
        3002
    ),
    'wDate' => array(
        '123',
        '234'
    ),
    'Hour' => array(
        12,
        13
    )
);

$keys = array_keys($a);//counting outer array

if(count($keys[0]) > 0)//checking if inner array has values at all
{
    $new_a = [];//initializing expecting array

    foreach($a[$keys[0]] as $k => $v)
    {
        for($i = 0; $i < count($keys); $i++)
        {
            $new_a[$k][$keys[$i]] = $a[$keys[$i]][$k];
        }
    }
}

//echo '<pre>', var_dump($new_a);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, That is what I actually want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.