How to add columns to CSV using PHP

后端 未结 2 1173
清酒与你
清酒与你 2021-01-05 20:22

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 m

相关标签:
2条回答
  • 2021-01-05 21:03

    Could you try using \r\n instead of \n ?

    0 讨论(0)
  • 2021-01-05 21:12

    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);
    
    ?> 
    
    0 讨论(0)
提交回复
热议问题