Search code examples
phparrayscsvfputcsv

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.


Solution

  • 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);