Force new line in CSV generated file in PHP

帅比萌擦擦* 提交于 2019-12-06 09:27:17

Do the following:

$csv_headers = array(array("Name", "Slope", "Length", "Size", "Max Length", "Location"));
$csv_full_data = array_merge($csv_headers, $csv_data);
var_dump($csv_full_data);
fputcsv($output, $csv_full_data);

And tell us the output.

What I did is I make the $csv_data array two dimensional instead of one and then I use foreach loop. Its a bit slow but working for me.

Here is the updated code

    $csv_data[$i][] = $_POST['name'][$i];
    $csv_data[$i][] = $_POST['slope'][$i];
    $csv_data[$i][] = $_POST['length'][$i];
    $csv_data[$i][] = $size;
    $csv_data[$i][] = $max_length;
    $csv_data[$i][] = $_POST['location'][$i];

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=file.csv');

    $output = fopen('php://output', 'w');
    fputcsv($output, array("Name", "Slope", "Length", "Size", "Max Length", "Location"));
    foreach ($csv_data as $value) {
        fputcsv($output, $value);
    }

This is the output which I was need.

Name    Slope   Length  Size    Max Length  Location
Name1    5       150    12"        500      location1 
Name2    8       350    12"        400      location12
Name3    16      326    12"        400      location3 
Name4    36      127    12"        400      location4 

what is the value of PHP_EOL make it "\r\n" and try

$csv_data[] = $_POST['location'][$i].PHP_EOL; change to 
$csv_data[] = $_POST['location'][$i]."\r\n";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!