I need to add columns to an existing csv file ,but i can't find any solution to the problem.I have used "\t" and chr(9) to create columns but no success so please help me by providing me the right solution if any one can
2
-
1can you show your current code?Tim– Tim2010-07-30 11:29:45 +00:00Commented Jul 30, 2010 at 11:29
-
file_put_contents("test.csv","\n".$dob[0].chr(9).$city[0].chr(9).$country[0],FILE_APPEND); my codeAnkur Mukherjee– Ankur Mukherjee2010-07-30 11:32:27 +00:00Commented Jul 30, 2010 at 11:32
Add a comment
|
2 Answers
Try this, and have a look at fgetcsv() and fputcsv() in the manual
<?php
$newCsvData = array();
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$data[] = 'New Column';
$newCsvData[] = $data;
}
fclose($handle);
}
$handle = fopen('test.csv', 'w');
foreach ($newCsvData as $line) {
fputcsv($handle, $line);
}
fclose($handle);
?>
1 Comment
user3750325
This worked well for me for smaller files, however with bigger files, this is at risk of hitting memory limits with the $newCsvData.