I'm trying to write the json object into a CSV file, but it's not working the file is empty.
any advice?
below my PHP code
//json to csv
$jsondata = '{"regNo":"92-212-98","tradName":"FEVADOL EXTRA TAB.","genericName":"PARACETAMOL, CAFFEINE","price":"5.300","strngth":"500, 65 mg","exD":"21/07","PRODREGSTATUS":"PRODNOTFOUND","authstatus":"1","marketingstatus":"1","productstatus":null}';
$json = json_decode($jsondata);
$fp = fopen('file.csv', 'w');
// if($fp == false){
// echo 'false open';
// }else{
// echo 'true open';
// }
foreach ($json as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
The second argument of fputcsv
function must be an array that shows cells of a row. More information: https://www.php.net/manual/en/function.fputcsv.php
The problem will solve if you change the for
part of code as following:
foreach ($json as $index => $field) {
fputcsv($fp, [$index, $field]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.