Very similar to the last one - I'm sure you should have been able to work it out. But, as an example for others...
################################################################################
# CSV COLUMNS #
################################################################################
$main = [ 1 => 'branch_id',
3 => 'dept_id',
5 => 'grp_id',
7 => 'category_id',
9 => 'sub_id'
];
$descrips = [ 10 => 'Wear Part', // if you have ids for these items, use those instead of text descriptions
11 => 'Consumables',
12 => 'Accessories',
13 => 'Electrical',
14 => 'Other Parts',
15 => 'Pipeline',
16 => 'Mixer Drum'
];
$qtrs = [ 17 => 'Q1',
18 => 'Q2',
19 => 'Q3',
20 => 'Q4'
];
################################################################################
# READ CSV AND CREATE DATA ARRAY #
################################################################################
$fp = fopen('test2.csv', 'r');
$hdr = fgetcsv($fp); // ignore header row
$data = [];
while ($line = fgetcsv($fp)) {
$reca = array_intersect_key($line, $main );
$recc = array_intersect_key($line, $qtrs );
foreach (array_intersect_key($line, $descrips ) as $k => $d) { // need a record for each description with a value
if ($d) {
$recb = [ $descrips[$k], (empty($d) ? 0 : $d ) ];
$data[] = vsprintf("(%d,%d,%d,%d,%d,'%s',%d,%d,%d,%d,%d)", array_merge($reca, $recb, $recc));
}
}
}
fclose($fp);
################################################################################
# WRITE RECORDS FROM ARRAY TO target TABLE #
################################################################################
$chunks = array_chunk($data, 2000);
foreach ($chunks as $ch) {
$pdo->exec("INSERT INTO target (branch_id, dept_id, grp_id, category_id, sub_id, description, value, Q1, Q2, Q3, Q4)
VALUES ". join(',', $ch)
);
}