2

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • What does one line of your `$rowcsv['reasonforabsence']` actually look like, please add a `print_r()` or `var_dump()` and what does `$overtime` look like – RiggsFolly Jul 31 '15 at 13:31
  • You could also try adding the last 2 parameter to `fputcsv($output, array( $overtime, $rowcsv['reasonforabsence']), ',', '"');` – RiggsFolly Jul 31 '15 at 13:35

2 Answers2

1

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);
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
  • Nice one. I had to explode the `$rowcsv['reasonforabsence']` string into an array but it works as I wanted. Ive been trying for about 6 hours to solve it so thank you very much! – Anthony Broadbent Jul 31 '15 at 13:39
0

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'])));
SiCK
  • 367
  • 4
  • 15