1

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.

1

1 Answer 1

0

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();
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but I am calling this arrays from API. Actually, there are at least 300 values.
That's just an example based on the data and code in your question. I was going to comment it
Thanks, you're right. I edited my post.
you can also do it in a single loop. build the array for the row just before the fputcsv you don't need the intermediate data array
for ($i = 0; $i < $size; $i++) {fputcsv($f, [ $first_array[$i], $second_array[$i] ], $delimiter);}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.