I need help writing my data in a csv file in a particular format. I have json data as follows
[
{
"name": "Abc",
"class": "12",
"date": "14-04-2018",
"answers": "question1 : answer1, q2 : a2, q3 : a3, q4 : a4"
}
]
I am storing the decoded version of this json in $details array. I have to write this data in a csv file. Here is my current code-
$filename = 'logs_'.$id.'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
$file = fopen('php://output', 'w');
$header = array("name","class","date","answers");
fputcsv($file, $header);
foreach ($details as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
I need this data in a particular format. I have attached the image containing both the current output and the required output. I want the answers to be written in separate rows as shown in the image.
(I have asked a similar question before but I had to delete it since it was ill-formed so please do not flag it) I want to achieve this without using any external libraries or pluggins. SO if you have any idea on how to achieve this then please let me know. Also, i was advised by one of the users on stackoverflow to use control break but i couldn't figure out a solution. If anyone else thinks thats the solution then please guide me