CSV读取第一行,编辑它,然后存储到数据库- PHP


CSV reading first line, editing it and then store into database - PHP

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

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

我不是很熟悉php代码,所以我很感激任何帮助,这就是我想做的。

CSV文件示例数据

Name Contact ID Email Address Status
Alice 0134222 21 alice@gmail.com 92 alice st available
claire 013423 24 claire@gmail.com 1 young st avail
victor 023429 31 victor@gmail.com 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数据。请帮助我,提前感谢,

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

fputcsv对于回写文件也很有用。

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

相关文章: