CSV到JSON.某些值为null


CSV to JSON. Some values are null

我构建了一个CSV到JSON的PHP文件。但它不会正常工作。值"faktor"始终为null。价值问题有时为空。我希望你能帮助我。谢谢!

PHP文件

    <?php
$row = 0;
$filename = "launeImTeam.csv";
$questions = array();
if (($handle = fopen($filename, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {   
        $questions[$row] = array(
            "question" => $data[0], 
            "typ" => $data[1],
            "faktor" => $data[2]
        );
        $row++;
    }
    fclose($handle);
}
$final = array("alleFragen"=>$questions);
echo json_encode($final); 
?>

这是CSV文件的数据。(德语)。

Wie sehen Sie Ihren zeitlichen Plan?;fuenf;Qualität;
Ich möchte Kaffee?;fuenf;Qualität;
Sind sie da???;jn;Qualität;
nein?;jn;Qualität;

这就是结果(

{
   "alleFragen":[
      {
         "question":"Wie sehen Sie Ihren zeitlichen Plan?",
         "typ":"fuenf",
         "faktor":null
      },
      {
         "question":null,
         "typ":"fuenf",
         "faktor":null
      },
      {
         "question":"Sind sie da???",
         "typ":"jn",
         "faktor":null
      },
      {
         "question":"nein?",
         "typ":"jn",
         "faktor":null
      }
   ]
}

对不起我的英语!:)

编辑:是因为äöü吗??

这是由于您的值未被编码为UTF-8所致。在数组上调用json_encode之前,请使用iconv()或utf8_encode将它们从原始编码(用于CSV文件)转换为UTF-8。任何无效的UTF-8值都将作为null返回。

json_encode的文档中对此进行了说明。

json_encode的默认行为是转义所有Unicode字符。

JSON_UNESCAPED_UNICODE选项与json_encode函数一起使用,如果json_last_error()函数出现任何错误。