如何使用php在csv文件的特定位置插入值

发布于 2024-09-14 04:27:09 字数 97 浏览 1 评论 0原文

是否可以使用 PHP 在 CSV 文件中的特定位置写入?

我不想在 CSV 文件末尾附加数据。但我想在 CSV 中已有值的行末尾添加数据。

提前致谢

Is it possible to write at a particular location in a CSV file using PHP?

I don't want to append data at the end of the CSV file. But I want to add data at the end of a row already having values in the CSV.

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧瑾黎汐 2024-09-21 04:27:09

不,由于文件系统的性质,不可能在文件中间插入新数据。
只能在末尾追加。

因此,唯一的解决方案是创建另一个文件,写入源文件的开始部分,附加一个新值,然后附加源文件的其余部分。最后将生成的文件重命名为原始名称。

No, it s not possible to insert new data in the middle of a file, due to filesystem nature.
Only append at the end is possible.

So, the only solution is to make another file, write a beginning part of source, append a new value, and then append the rest of the source file. And finally rename a resulting file to original name.

路弥 2024-09-21 04:27:09

就这样吧。完整的工作代码:

<?php
//A helping function to insert data at any position in array.
function array_insert($array, $pos, $val)
{
    $array2 = array_splice($array, $pos);
    $array[] = $val;
    $array = array_merge($array, $array2);

    return $array;
}

//What and where you want to insert
$DataToInsert = '11,Shamit,Male';
$PositionToInsert = 3;

//Full path & Name of the CSV File
$FileName = 'data.csv';

//Read the file and get is as a array of lines.
$arrLines = file($FileName);

//Insert data into this array.
$Result = array_insert($arrLines, $PositionToInsert, $DataToInsert);

//Convert result array to string.
$ResultStr = implode("\n", $Result);

//Write to the file.
file_put_contents($FileName, $ResultStr);
?>

There you go. Complete working code:

<?php
//A helping function to insert data at any position in array.
function array_insert($array, $pos, $val)
{
    $array2 = array_splice($array, $pos);
    $array[] = $val;
    $array = array_merge($array, $array2);

    return $array;
}

//What and where you want to insert
$DataToInsert = '11,Shamit,Male';
$PositionToInsert = 3;

//Full path & Name of the CSV File
$FileName = 'data.csv';

//Read the file and get is as a array of lines.
$arrLines = file($FileName);

//Insert data into this array.
$Result = array_insert($arrLines, $PositionToInsert, $DataToInsert);

//Convert result array to string.
$ResultStr = implode("\n", $Result);

//Write to the file.
file_put_contents($FileName, $ResultStr);
?>
凉风有信 2024-09-21 04:27:09

从技术上讲Col 。 Shrapnel的回答是绝对正确的。

您的问题是您不想处理所有这些文件操作只是为了更改某些数据。我同意你的看法。但你在错误的层面上寻找解决方案。把这个问题放在一个更高的层面上。创建一个代表 CSV 数据库中的实体的模型。修改模型的状态并调用其 save() 方法。该方法应该负责以 CSV 格式写入模型的状态。

尽管如此,您仍然可以使用 CSV 库来为您抽象出低级操作。例如,parsecsv-for-php 允许您定位特定单元格:

$csv = new parseCSV();
$csv->sort_by = 'id';
$csv->parse('data.csv');
# "4" is the value of the "id" column of the CSV row
$csv->data[4]['firstname'] = 'John';
$csv->save();

Technically Col. Shrapnel's answer is absolutely right.

Your problem is that you don't want to deal with all these file operations just to change some data. I agree with you. But you're looking for the solution in a wrong level. Put this problem in a higher level. Create a model that represents an entity in your CSV database. Modify the model's state and call its save() method. The method should be responsible to write your model's state in CSV format.

Still, you can use a CSV library that abstracts low level operations for you. For instance, parsecsv-for-php allows you to target a specific cell:

$csv = new parseCSV();
$csv->sort_by = 'id';
$csv->parse('data.csv');
# "4" is the value of the "id" column of the CSV row
$csv->data[4]['firstname'] = 'John';
$csv->save();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文