Well good/bad/ugly this is what I came up with…
Note: I am showing $row FOR loop on my sample. You would use yours.
I also did not include the id or hash field in my $fields array.
ALSO as I did not see a sample of $rowData or $headings arrays I added the extra key [0] that I am expecting… Adjust as needed.
$highestRowIndex = $worksheet->getHighestRow();
$highestColumnIndex = $worksheet->getHighestColumn();
for ($row = 1; $row <= $highestRowIndex; $row++) {
$headings = $worksheet->rangeToArray('A1:' . $highestColumnIndex . 1,null, true, false);
$rowData = $worksheet->rangeToArray('A' . $row . ':' . $highestColumnIndex . $row, null, true, false);
$fields = array(
'title'
, 'surname'
, 'first_name'
, 'middle_name'
, 'gender'
, 'dob'
, 'marital_status'
, 'occupation'
, 'phone_number'
, 'company_name'
, 'registration_type'
, 'registered_by'
, 'tax_id'
, 'office_address'
, 'office_city'
, 'temp_reg'
, 'workplace_category'
, 'active'
, 'monthly_gross'
, 'NHF'
, 'NHIS'
, 'NSITF'
, 'basic_salary'
, 'grade'
, 'designation'
, 'pension'
, 'gratuity'
);
$matches = array_intersect($headings[0],$fields);
$values = array();
foreach($fields as $k => $v):
if($v == "occupation"){
$values[] = $textfile_occupation;
}elseif($v == "company_name"){
$values[] = $institution;
}elseif($v == "registered_by"){
$values[] = $UserLogin;
}else{
$values[] = (in_array($v,$matches) ? $rowData[0][array_search($v,$matches)] : '');
}
endforeach;
$row_values = implode(',',$values);
$stmt =$connect->prepare("INSERT INTO tem_treg2 (title,surname,first_name,middle_name,gender,dob,marital_status,occupation,phone_number,company_name,registration_type,registered_by,tax_id,office_address,office_city,temp_reg,workplace_category,active,monthly_gross,nhf,nhis,nsitf,basic_salary,grade,designation,pension,gratuity) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssssssssssssssssss",$row_values);
$stmt->execute();
}
EDITED: NOTE the $fields array could be written to hold xls column headings as long as they still match the order of table fields, e.g. ‘first_name’ changed to ‘First Name’