Codeigniter multidimensional array insert into database Codeigniter multidimensional array insert into database codeigniter codeigniter

Codeigniter multidimensional array insert into database


Use this rather your own function

function insertfiles($arr_data){    foreach($array_data as $a){        $data = array(               'Firstname' => $a['A'],               'Lastname' => $a['B'],               'Phone'=>$a['C'],               'Fax' =>$a['D']            );            $this->db->insert('test', $data);     }}


There are two cases for this one for case 1

If the array you pass does not contain a named key, check the function below

$table_array_index_key = array( //if your array does not have a named key.    0 => array( //first row        0 => 'first_name 1',        1 => 'last_name 1',        2 => 'phone 1',        3 => 'fax 1'        ),    1 => array( //second row        0 => 'first_name 2',        1 => 'last_name 2',        2 => 'phone 2',        3 => 'fax 2'        ),    );$row = array();$columns = array();for($x=0;$x<count($table_array_index_key);$x++){        $row = array(            'Firstname' => $table_array_index_key[$x][0], //$table_array[1][0] means table_array row 2 column 1              'Lastname' => $table_array_index_key[$x][1],            'Phone' =>  $table_array_index_key[$x][2],            'Fax' => $table_array_index_key[$x][3]         );        array_push($columns,$row);        $rows = array();}echo "<pre>";print_r($columns);

else if your array contains named keys, check this out.

$table_array_index_name = array( //if your array have a named key.    0 => array( //first row        'A' => 'first_name 1',        'B' => 'last_name 1',        'C' => 'phone 1',        'D' => 'fax 1'        ),    1 => array( //second row        'A' => 'first_name 2',        'B' => 'last_name 2',        'C' => 'phone 2',        'D' => 'fax 2'        ),    );$rows = array();$columns = array();$arrayNames = array('A','B','C','D'); for($x=0;$x<count($table_array_index_name);$x++){        $row = array(            'Firstname' => $table_array_index_name[$x][$arrayNames[0]], //note $arrayNames[0] = A            'Lastname' => $table_array_index_name[$x][$arrayNames[1]],            'Phone' =>  $table_array_index_name[$x][$arrayNames[2]],            'Fax' => $table_array_index_name[$x][$arrayNames[3]]         );        array_push($columns,$row);        $rows = array();}echo "<pre>";print_r($columns);

For which you can loop further like this one:

$table_array_index_name = array( //if your array have a named key.    0 => array( //first row        'A' => 'first_name 1',        'B' => 'last_name 1',        'C' => 'phone 1',        'D' => 'fax 1'        ),    1 => array( //second row        'A' => 'first_name 2',        'B' => 'last_name 2',        'C' => 'phone 2',        'D' => 'fax 2'        ),    );$rows = array();$columns = array();$arrayNames = array('A','B','C','D');$dbFieldName = array('Firstname','Lastname','Phone','Fax');for($x=0;$x<count($table_array_index_name);$x++){    for($y=0;$y<count($arrayNames);$y++){        $row[$dbFieldName[$y]] = $table_array_index_name[$x][$arrayNames[$y]];    }        array_push($columns,$row);        $rows = array();}echo "<pre>";print_r($columns);

which will return the following data:

Array(    [0] => Array        (            [Firstname] => first_name 1            [Lastname] => last_name 1            [Phone] => phone 1            [Fax] => fax 1        )    [1] => Array        (            [Firstname] => first_name 2            [Lastname] => last_name 2            [Phone] => phone 2            [Fax] => fax 2        ))

Personally I prefer the for loop because we can have more freedom and control when manipulating arrays.

For the $this->db->insert_batch function, please check out the code igniter active record documentation:https://ellislab.com/codeigniter/user-guide/database/active_record.html