note 124372 deleted from function.json-encode by crell
| From: | crell@php.net | Date: | Thu, 30 Jun 2022 19:42:47 +0000 |
| Subject: | note 124372 deleted from function.json-encode by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218786@lists.php.net to get a copy of this message | ||
Note Submitter: jugcosta at ymail dot com
----
<?php
error_reporting(0);
ini_set('display_errors', FALSE);
$file= __DIR__ . '/utf8.csv';
function csvtojson($inputFile,$delimiter)
{
if (($handleFile = fopen($inputFile, 'r')) == false) {
throw new Exception("Não foi possÃvel abrir o arquivo para importar:
'$file'");
}
$dados = array();
while (($data = fgetcsv($handleFile, 4000, $delimiter)) !== false) {
$data = array_map('trim', $data);
if (! isset($keys)) {
$keys = array_map('trim', $data);
continue;
}
$dados[] = array_combine($keys, $data);
}
$result = json_encode($dados, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |JSON_NUMERIC_CHECK
| JSON_PRETTY_PRINT);
echo $result;
$arquivo = "data_".date('ymdhis').".json";
$fp = fopen($arquivo, "a+");
fwrite($fp, $result);
fclose($handleFile);
fclose($fp);
}
$jsonresult = csvtojson($file, ";");
?>