krzysztof krzysztof - 6 months ago 56
PHP Question

php CSV headers for columns

I am the beginer in programing,
could anyone help me , how can I write in php headers of the columns for generated CSV file . Like I want to set header ( name, surname , address)
enter image description here


You could read the .csv file into an array with file(), then create a new array, assigning the headers as the first entry, followed by writing all lines from the csv file to the new array. Finally, save your new CSV.

$oldcsv = file('sample.csv');
$csv = array();
$csv[] = "name,surname,address";
foreach($oldcsv as $v){
   $csv[] = $v;