I have a csv file that has data like
shelfmark, centuries
foo-0001, 2000s;
bar-1234, 1200s, 1300s;
baz-IK-1234, 0100s, 0200s, 0300s;
and so on...
I want to import the data and convert it to a format that has only two columns:
shelfmark, century
foo-0001, 2000s;
bar-1234, 1200s;
bar-1234, 1300s;
baz-IK-1234, 0100s;
baz-IK-1234, 0200s;
baz-IK-1234, 0300s;
and so on...
And write it in a new csv-file.
I need help with the code of creating new rows if a row contains more than one century value.
My attempt:
<?php
$csv = array_map('str_getcsv', file($file));
array_walk($csv, function(&$a) use ($csv) {
// check if row has more than two column entries
if
// if so then split the first century value into a new row
// do so until no more century value is in the row left and continue with next row
$data = array_combine($csv[0], $a);
});
// write into new csv file
$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
$val = explode(",", $line);
fputcsv($fp, $val);
}
fclose($fp);
?>