在 php 中使用 fputcsv 添加带有计数器的 1 行并首先更改为最后 Header

Add 1 Line with counter and change first with last Header using fputcsv in php

我写了一个小脚本,它将从 URL 中获取 CSV ...添加一行并将其导入到 mysql 数据库中...

一切正常,但有一件事

如何更改标题的内容

我用它来将我的内容写入新文件

$counter = 0;               
$data[53] = $counter++;

           fputcsv($handle2,$data,$delimiter = ";", $enclosure = '"');

这将在我的 CSV 文件的最后一行之后添加一个带有计数器的行(我的 CSV 没有唯一 ID,因此我必须添加一个,然后才能根据需要导入 CSV)

它工作正常,但我必须通过 shell 直接在 CSV 文件中更改一些内容,但 id 应该直接在脚本中完成

新添加的行在第一个标题中以 0 开头,但它应该以 uniqueid 或其他名称开头,而不是计数器值...但我不知道该怎么做。

instead of

0
1
2
3++

I need something heading like

uniqueid
1
2
3++

我还想将 $data[1] 的标题更改为 $data[53]

的标题
///////////UPDATE////////
    if($loadfile == true){
    if (($handle1 = fopen("http://someurl.com/csv.csv", "r")) !== FALSE) {
        if (($handle2 = fopen("test.csv", "w")) !== FALSE) {
            while (($data = fgetcsv($handle1, 99999, ";", $enclosure = '"')) !== FALSE) {
               $data[] = $counter++;
               fputcsv($handle2,$data,$delimiter = ";", $enclosure = '"');

            }
            fclose($handle2);
        }
        fclose($handle1);
        echo '<script type="text/javascript">alert("Succes ;)")</script>';
    }
    }

所以这很简单

if ($loadfile == true) {
    if (($handle1 = fopen("http://someurl.com/csv.csv", "r")) !== FALSE) {
        if (($handle2 = fopen("test.csv", "w")) !== FALSE) {
            $counter = 0;
            while (($data = fgetcsv($handle1, 99999, ";", $enclosure = '"')) !== FALSE) {

                // if you want to have the id at the last column
                $data[] = ($counter == 0) ? "uniqueid" : $counter;

                // or alternatively for the id at the first column
                //($counter == 0) ? array_unshift($data, "uniqueid") : array_unshift($data, $counter);

                fputcsv($handle2,$data,$delimiter = ";", $enclosure = '"');
                $counter++;
            }
            fclose($handle2);
        }
        fclose($handle1);
        echo '<script type="text/javascript">alert("Succes ;)")</script>';
    }
}