Home > Software engineering >  How do I create CSV file with PHP arrays?
How do I create CSV file with PHP arrays?

Time:10-21

I read official phpnet resources but I can't.

I have 2 arrays.

First array :

Array
(
    [0] => KDS-1655B
    [1] => KS-9916
    [2] => KDS-1197
    [3] => KDS-4164
    [4] => MRK-1994
    [5] => KDS-9773
) // LONG ARRAY, THIS IS EXAMPLE

Second array:

Array
(
    [0] => PRODUCTNAME1
    [1] => PRODUCTNAME2
    [2] => PRODUCTNAME3
    [3] => PRODUCTNAME4
    [4] => PRODUCTNAME5
    [5] => PRODUCTNAME6
) // LONG ARRAY, THIS IS EXAMPLE

Now, I want to create CSV file with 2 headers and 2 columns. I tried this:

$data = array(
    array($first_array, $second_array), // ARRAY CONTENTS
$filename = 'mycsv' . '.csv';
$delimiter = ',';

$f = fopen('php://memory', 'w');

$headings = array('firstheader', 'secondheader',); // HEADERS

fputcsv($f, $headings, $delimiter);

foreach($data as $row) {
    fputcsv($f, $row, $delimiter);
}


fseek($f, 0);

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');

fpassthru($f);

fclose($f);

exit();

But all arrays are generating in single header. What can I do about this issue? Thanks, Regards.

CodePudding user response:

For explanations purpose, I'll reduce the data to:

$first_array:

Array {
  [0]=> KDS-1655B
  [1]=> KS-9916
}

$second_array:

Array {
  [0]=> PRODUCTNAME1
  [1]=> PRODUCTNAME2
}

The way you fill $data is wrong, because $data = array($first_array, $second_array); will give you:

Array {
  [0]=> Array {
    [0]=> KDS-1655B
    [1]=> KS-9916
  }
  [1]=> Array {
    [0]=> PRODUCTNAME1
    [1]=> PRODUCTNAME2
  }
}

While you need:

Array {
  [0]=> Array {
    [0]=> KDS-1655B
    [1]=> PRODUCTNAME1
  }
  [1]=> Array {
    [0]=> KS-9916
    [1]=> PRODUCTNAME2
  }
}

If I remember correctly, PHP doesn't provide any function to generate this kind of concatenation, you have to code it yourself, for example like this:

$data = array();
$size = count($first_array);
for ($i = 0; $i < $size; $i  ) {
    $data[] = array( $first_array[$i], $second_array[$i] );
}

And you're done. The resulting CSV will be something like:

KDS-1655B,PRODUCTNAME1
KS-9916,PRODUCTNAME2

Your fixed code

$filename = 'mycsv' . '.csv';
$delimiter = ',';
$data = array();
$size = count($first_array);
for ($i = 0; $i < $size; $i  ) {
    $data[] = [ $first_array[$i], $second_array[$i] ];
}

$f = fopen('php://memory', 'w');

$headings = array('firstheader', 'secondheader'); // HEADERS

fputcsv($f, $heading, $delimiter);

foreach($data as $row) {
    fputcsv($f, $row, $delimiter);
}
fseek($f, 0);

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');

fpassthru($f);
fclose($f);
exit();

Another code

$csv_filename = 'mycsv.csv';
$csv_header = array('firstheader', 'secondheader');

header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $csv_filename . '";');

$fp = fopen('php://memory', 'w');

fputcsv($fp, $csv_header);

$size = count($first_array);
for ($i = 0; $i < $size; $i  ) {
    fputcsv($fp, [ $first_array[$i], $second_array[$i] ]);
}

fseek($fp, 0);
fpassthru($fp);

fclose($fp);

exit();
  • Related