CSV 读取第一行,对其进行编辑,然后存储到数据库中 - PHP

发布于 2024-11-02 07:31:13 字数 1308 浏览 0 评论 0原文

我是 php 新手,目前正在开发一个涉及 csv 的项目。我过去没有编写读取 csv 文件的脚本的经验。所以请不要忽略我的帖子并原谅我。

摘要:该任务要求我使用 php 脚本读取 CSV 文件,重命名第一行(标题,然后插入数据库)。

我对 php 代码不是很熟悉,所以我希望得到任何帮助,这就是我想做的。

CSV 文件的示例数据

Name Contact ID Email Address Status
Alice 0134222 21 [email protected] 92 alice st available
claire 013423 24 [email protected] 1 young st avail
victor 023429 31 [email protected] 15/8 johnson st not available

创建读取 CSV 文件的函数。

function parse_csvdata($filename) {
  //read the first line of the csv file only (the title)
  //rename the title to User_Name, User_Contact, User_ID (instead of Name, Contact, ID)

  //temporary store it in an array as TITLE

  //read the rest of the data (after the first line)
  //check the data in the title column. I want to change all avail to Available

  then create a new CSV out of this
}

我已经完成了将 csv 文件解析到 mysql 中的脚本。但我只是不知道如何重命名/重新格式化 csv 数据。请帮助我,预先感谢,

I'm new with php and I am currently working on a project which involve of csv. I do not have any past experience of writing a script to read csv files. So please do not ignore my post and do excuse me.

Summary: The task require me to read a CSV file with php script, rename the 1st line (Title, and insert into database).

I am not very familiar with php code, so I would appreciate any help, and this is what I would like to do.

Example data of the CSV File

Name Contact ID Email Address Status
Alice 0134222 21 [email protected] 92 alice st available
claire 013423 24 [email protected] 1 young st avail
victor 023429 31 [email protected] 15/8 johnson st not available

create a function that reads a CSV file.

function parse_csvdata($filename) {
  //read the first line of the csv file only (the title)
  //rename the title to User_Name, User_Contact, User_ID (instead of Name, Contact, ID)

  //temporary store it in an array as TITLE

  //read the rest of the data (after the first line)
  //check the data in the title column. I want to change all avail to Available

  then create a new CSV out of this
}

I already finish the script to parse csv files into mysql. But I just dunno how to rename/reformat the csv data. Please help me, thanks in advance,

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

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

发布评论

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

评论(2

揽月 2024-11-09 07:31:13

不确定您的解析需要什么,但我建议使用 fgetcsv 进行解析。如何操作和推送回数据库取决于您。

fputcsv 对于回写也很有用到文件。两个文档页面都有使用示例。

Not sure what your parsing entails, but i recommend using fgetcsv for parsing. How you manipulate and push back to a database is up to you.

fputcsv is also useful for writing back to the file. Both doc pages have examples of usage.

夜无邪 2024-11-09 07:31:13

您不需要编写整个代码来解析并将值放入数组中。您可以使用 ParseCSV 库来做到这一点。您可以将值放入数组并对其进行操作。

You don't need to write the entire code to parse and get the values into an array.You can use ParseCSV library to do it. You can take the values to an array and manipulate it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文