CSV to JSON with PHP?

前端 未结 6 1446
遥遥无期
遥遥无期 2020-12-31 13:48

I need to convert a CSV file to JSON on the server using PHP. I am using this script which works:

function csvToJSON($csv)          


        
6条回答
  •  感动是毒
    2020-12-31 14:26

    Well there is the json_encode() function, which you should use rather than building up the JSON output yourself. And there is also a function str_getcsv() for parsing CSV:

    $array = array_map("str_getcsv", explode("\n", $csv));
    print json_encode($array);
    

    You must however adapt the $array if you want the JSON output to hold named fields.

提交回复
热议问题