我有JSON格式的内容,例如:
{"rand":"7542868","action":"get","identity":{"name":"Jack","password":"secret"},"key":"o8sh769f79"}
而且我知道怎么破译它。但是如何在txt文件中加入像下面这样优美的格式呢?
{
"rand":"7542868",
"action":"get",
"identity":
{
"name":"Jack",
"password":"secret"
},
"key":"o8sh769f79"
}
PHP Version 5.5.27
我创建的方法:
static function ApiLog($type ,$data)
{
$data = json_encode($data, JSON_PRETTY_PRINT);
$myfile = fopen(__DIR__."/log/".date('Y-m-d').".txt", "w") or die("Unable to open file!");
$entry = date("H:i:s")." ".$type." ".$data."
";
fwrite($myfile, $entry);
fclose($myfile);
}
在$data中输入:
{"rand":"7542868","action":"get","identity":{"name":"Jack","password":"secret"},"key":"o8sh769f79"}
输出的文件依然是静止直列:
11:34:23 <- "{\"rand\":\"7542868\",\"action\":\"get\",\"identity\":{\"name\":\"Jack\",\"password\":\"secret\"},\"key\":\"o8sh769f79\"}"
$file = fopen("yourfile.txt","w");
fwrite($file, json_encode($data_array, JSON_PRETTY_PRINT));
fclose($file);