1

I have a php application where I import the data from excel sheet to the database. I am using phpExcel for this.

I have achieved a simple import functionality for this.

Now, my problem is when I import data form excel sheet to the database in particular format. Like,

Table name:-Mytable

id | name | surname | email | date |

and the excel file I am using to import have a same format. But a format can change Like,

id | surname | name | date | email |

how can I map the columns from excel file to the database table so that correct data should enter into the data table.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

-1

Read the headers first, and loop over each header entry to get the column names, then when you're looping your row you can build an associative array of StdClass object of that row appropriately mapped

$headers = $objPHPExcel->getActiveSheet()->toArray('A1');
$highestRow = $sheet->getHighestRow();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 
    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row);

    //  Map the row data array to the headers array
    $dataMapArray = array_combine(
        $header,
        $rowData
    );
    // If you prefer objects, then convert that associative array to an object
    $dataMapObject = (object) $dataMapArray;
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385