我使用 PHP 创建了一个包含一列的 CSV 文件。我在循环中迭代地执行此操作。 我现在想要的是在这个 CSV 中添加第二列。我想在循环结束后这样做。 可以用 php 做到这一点吗?
for ($number = 1; $number <= 26; $number++) {
#create CSV With one column
}
#append one more column to existing CSV
最佳答案
你可以这样做:
$a = file('csv.csv');// get array of lines
$new = '';
foreach($a as $line){
$line = trim($line);// remove end of line
$line .=";new column";// append new column
$new .= $line.PHP_EOL;//append end of line
}
file_put_contents('csv.csv', $new);// overwrite the same file with new data
关于php - 使用 PHP 在现有 CSV 中添加第二列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248336/