【发布时间】:2011-04-17 06:09:18
【问题描述】:
我是 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 数据。请帮助我,提前谢谢,
【问题讨论】:
-
你应该拥有你的代码。
-
您的问题真的是 CSV 中的列名与您的数据库表的列名不匹配吗?如果是这样,我只需创建一个查找数组作为配置,将 CSV 中的名称映射到表列名称。