if (isset($_SESSION['user'])) {
$plik = 'users/'.$_SESSION['user'].'/settings.json';
$file = fopen($plik, "a+");
@fclose($plik);
$jsonString = file_get_contents($plik);
$data = json_decode($jsonString, true);
if ((isset ($_POST['s_pm'])) && $_POST['s_pm']=="on")
$data["set_friendreq"] = 1; else $data["set_friendreq"] = 0;
if (isset($_POST['dsp']))
$data["set_displayname"] = $_POST['dsp'];
$newJsonString = json_encode($data);
file_put_contents($plik, $newJsonString);
}
所有内容都保存到 JSON 文件中没有任何问题(例如set_friendreq
因为它是一个数字),但是当我尝试发送字符串时,我的 JSON 文件中会出现"0"(set_displayname
)。为什么?
JSON 文件:
{"set_friendreq":0,"set_displayname":"0","lang":"2"}
> 对于set_friendreq,它是 0,因为类型是 Integer,u 在字符串代码中初始化它们。
$data["set_friendreq"] = 1; else $data["set_friendreq"] = 0;
$_POST - 它是数组字符串,在 u 中尝试使用 json_decode u 看到"0"。因为 0 它是字符串而不是整数。U 对变量类型有问题。