Example CSV:
Sally Whittaker,2018,McCarren House,312,3.75
Cushing House,148,3.52
Prescott House,17-D,3.20
Sandy Allen,2019,Oliver House,108,3.48
Second and third line - first two items missing.
Code:
$file=file("csv.csv");
$str='';
foreach ($file as $line) {
$liner=explode(',',$line);//use your own separator
if(count($liner)<5) {
array_unshift($liner, 'item1','item2');//put items at the start
$str.= implode(',', $liner);
}
else {
$str.=implode(',', $liner);
}
}
file_put_contents('csv.csv', $str);
Output CSV:
Sally Whittaker,2018,McCarren House,312,3.75
item1,item2,Cushing House,148,3.52
item1,item2,Prescott House,17-D,3.20
Sandy Allen,2019,Oliver House,108,3.48
EDIT:
if csv (probably), have this structure:
Sally Whittaker,2018,McCarren House,312,3.75
,ddd,Cushing House,148,3.52
,xxxx,Prescott House,17-D,3.20
Sandy Allen,2019,Oliver House,108,3.48
code could be:
$file=file("csv.csv");
$str='';
foreach ($file as $line) {
$liner=explode(',',$line);
if($liner[0]=='') { //check if first item is empty!
$liner[0]='item';
}
$str.=implode(',', $liner);
}
file_put_contents('csv.csv', $str);