0

I am trying to export an array to csv, but my csv contains only one row with many fields (columns). I want to achieve to export into rows my data. My data looks like when I dump:

array(2) { [0]=> string(10) "something1" [1]=> string(18) "something2" }

here is the php code: ($names is my array)

$list = array ($names);
$fp = fopen('download.csv', 'w');
    foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

Thank you in advance!

EDIT: now looks like: enter image description here how to achieve this:enter image description here

5
  • Possible duplicate of PHP Array to CSV Commented Jul 7, 2017 at 16:49
  • And what is the problem? Commented Jul 7, 2017 at 16:55
  • I updated my question :) @u_mulder Commented Jul 7, 2017 at 17:04
  • you mean, you will have one column with multiple rows ? Commented Jul 7, 2017 at 17:17
  • yes @Ravi, Acieve to have multiple rows from array than one row with multiple columns... Commented Jul 7, 2017 at 17:19

1 Answer 1

2

There's no need to put the $names into an array to accomplish what you want.

$fp = fopen('download.csv', 'w');
foreach ($names as $name) {
    fputcsv($fp, [$name]);
}
fclose($fp);
Sign up to request clarification or add additional context in comments.

1 Comment

@almostokey I've updated it to reflect your desired outcome.

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.