fputcsv - only one column
Solution 1
This this:
fputcsv($fp, array_values($list));
EDIT:
Use this to get it instantly working with excel:
fputcsv($fp, array_values($list), ';', ' ');
EDIT 2: The default character that is used as the field-separator in excel is set by the locale settings of windows. That means: If you export a csv-file with german locale settings will cause problems with excel in an us-environment... Another thread covered this topic.
Solution 2
Try this
fputs($fp, implode(array_keys($list), ',')."\n");
fputs($fp, implode(array_values($list), ',')."\n");
Matthias
Updated on June 15, 2022Comments
-
Matthias almost 2 years
I want to save parts (and differently arranged) of my mysql database in a CSV file. For that I wrote this piece of code:
$headerDisplayed = false; foreach ($arr as &$currName) { $result2 = mysql_query("SELECT * FROM Experiment Where Person='$currName'"); $i = 1; while ($row2 = mysql_fetch_array($result2)) { $list['Name'] = $currName; $list['Studie'] = $row2['Studie']; if(strcmp($row2['Antwort'],$row2['Korrekt']) == 0) { $list["A $i"] = '0'; } else { $list["A $i"] = '1'; } $i++; } if ( !$headerDisplayed ) { // Use the keys from $data as the titles fputcsv($fp, array_keys($list)); $headerDisplayed = true; } fputcsv($fp, $list); } fclose($fp);
Unfortunately this does not what I wanted it to do. My plan was that each entry of
$list
(e.g.$list["A 1"]
or$list["A 15"]
) has its own column. And then the call offputcsv
should create a new line and again one column for each$list
entry. I create new rows with eachfputcsv
call, just as expected, but the complete$list
array is in the first (most left) column.What do I have to do to solve this issue?
ADDED: I get something like:
[1,0,0,0,1,1,0,0] [] [] [] [] [] [] [] [1,0,1,1,0,1,0,0] [] [] [] [] [] [] [] [0,1,1,1,1,1,0,1] [] [] [] [] [] [] []
but what I want is more like:
[1] [0] [0] [0] [1] [1] [0] [0] [1] [0] [1] [1] [0] [1] [0] [0] [0] [1] [1] [1] [1] [1] [0] [1]
-
Matthias almost 12 yearsThis only leads to [Array] in each row of the csv file :(
-
Ron almost 12 yearsBad idea! What, if the input is somethink like: "Surname, Firstname" ?
-
Matthias almost 12 yearsThat would not be the case, even $list['Name'] is in fact not a name but a coded integer number referring to a name. But still this doesn't change anything. I add something to my question.
-
Ron almost 12 yearsYour code just handles the header-column. If you apply this on the csv-content, a comma in a field could possibly occure...
-
Matthias almost 12 yearsI can run it, and it returns a CSV file which has "This is a test,"""This"" is a test","This is a 'test'","This is a" in its first row first column and then "test" in next row. And this repeated a lot of times.
-
Ron almost 12 years@Justin Why whould one use array_map together with fputcsv just wo wrap "" aroung fields? Just use fputcsv($f, $array, ';', '"');
-
Matthias almost 12 yearsI tested it anyway. It messes everything completely up^^ But thank you very much for your trys and your efford :)
-
Jinzhao Huo over 11 yearsCool! change delimiter to ";" works perfect! Thank you very much! Still I am curious why using "," as the delimiter doesn't work???
-
commonpike over 10 yearsWhy is there a ' ' space for enclosure ?