How to edit .csv header row

强颜欢笑 提交于 2021-01-29 17:54:32

问题


I´m using a script to export Products from a shop to one .csv file. Now I need to change the header row to make it work for me.

I was searching a long time but didn't get it how to make it work. maybe I'm just to silly for that.

Lets say my CSV looks like this:

  1. Heading1 Heading2 Heading3
  2. value1 value2 value3

Now I need to open that csv file, change just the Headings, close and save it.


回答1:


$f = "file.csv";

$new_heading =  "World,Mama,Euro";
$new_heading.="\n";

// read into array
$arr = file($f);

// edit first line
$arr[0] = $new_heading;

// write back to file
file_put_contents($f, implode($arr));


来源:https://stackoverflow.com/questions/22618065/how-to-edit-csv-header-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!