Anthony Broadbent
Anthony Broadbent

Reputation: 403

PHP array to CSV issues

I have an array: $rowcsv['reasonforabsence'] this contains an undetermined amount of values. I want to output the values of this array to a CSV along with other values like this:

fputcsv($output, array( $overtime, $rowcsv['reasonforabsence']));

But when I do this the CSV outputs:

|6|"Holiday Full Day,Sick Full Day,Sick AM,Sick PM,Holiday Full Day,Holiday AM,Holiday PM,Absent Full Day,Absent AM|`

So basically instead of the $rowcsv['reasonforabsence'] array putting the values into new cells the array is just putting all the values into 1 cell. I have tried:

$reasonforabsence = explode( ',', $rowcsv['reasonforabsence'] );

$reasonforabsence = implode('","', $reasonforabsence);

But I get the output:

"Holiday Full Day"",""Sick Full Day"",""Sick AM"",""Sick PM"",""Holiday Full Day"",""Holiday AM"",""Holiday PM"",""Absent Full Day"",""Absent AM"

And everything still appears in 1 cell just this time with the quotes.

Upvotes: 2

Views: 110

Answers (2)

SiCK
SiCK

Reputation: 367

With array( $overtime, $rowcsv['reasonforabsence']) you construct an array with two entries. Accordings $rowcsv['reasonforabsence'] is a plain comma separated values string, you have to explode() this into an array and merge it with your $overtime value :

fputcsv($output, array_merge(array($overtime), explode(',', $rowcsv['reasonforabsence'])));

Upvotes: 0

FuzzyTree
FuzzyTree

Reputation: 32392

Combine $overtime and $rowcsv['reasonforabsence'] into a single array using array_merge and pass the combined array to fputcsv

$row = array_merge(array($overtime),explode(',',$rowcsv['reasonforabsence']));
fputcsv($output,$row);

Upvotes: 1

Related Questions