سؤال

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.

هل كانت مفيدة؟

المحلول

$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));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top