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.

2
  • 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 Commented Jul 31, 2015 at 13:31
  • You could also try adding the last 2 parameter to fputcsv($output, array( $overtime, $rowcsv['reasonforabsence']), ',', '"'); Commented Jul 31, 2015 at 13:35

2 Answers 2

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

1 Comment

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!
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'])));

Comments

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.