猿问

json_encode 创建第二个数组 [null,null,null,null,null]

我有一些基本代码,可以从上一页的表单中获取结果,并将其编码为文本文件。出于某种原因,它对其进行了正确编码,然后创建了一个只有空值的第二个数组。


(每个数组中有相同数量的值)


老实说,我不知道是什么原因造成的。


这是编码代码:


<?php

$name = $_POST['name'];

$email = $_POST['email'];

$date = $_POST['date'];

$destination = $_POST['destination'];

$msg = $_POST['msg'];


//TODO the file write here VV, use 'a' instead of 'w' too ADD to the file instead of REWRITING IT.

$arr = [$name,$email,$date,$destination,$msg];


$write = json_encode($arr);

echo $write;


$file = fopen('data.txt', 'a');

fwrite($file, $write);

fclose($file);

// echo $arr[];

 ?>

这是 .txt 文件中的结果:


["Simon","example@example.com","0101-01-01T01:01","Ohio","Message here"][null,null,null,null,null]

(如果有帮助,它将它们写在同一行上)


我不想要这个空数组,因为它会搞乱我需要做的一些事情。有什么想法吗


阿晨1998
浏览 130回答 2
2回答

缥缈止盈

问题不在于 json_encode。您收到两个单独的 POST 请求 - 一个具有正确的数据,随后的没有任何数据的请求作为空值附加到文件中。您需要在客户端调试为什么要发送两个请求
随时随地看视频慕课网APP
我要回答