在json文件中的长行写的Php数组 [英] Php array written on long line in json file

查看:67
本文介绍了在json文件中的长行写的Php数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际的json txt文件内容与此类似:

the actual json txt file content is similar to this:

[ 
{ "name": "Video 0", "comment": "3RtYLZaUUdU" }, 
{ "name": "Video 1", "comment" : "QrIGoya6BcE" }, 
{ "name":"Video 2", "comment": "KQbr2OGydJU" } 
]
;










<pre>$post_data = $_POST['data'];  //user comment input text
$file = "newfile.txt";  //actual json text file, the content is above
$content = file_get_contents($file);
$arr =(json_decode($content, true)); //converts json to php associative array
print_r($arr);
echo "<br><br>";
$AssArr =array(
        "name" => "Video 3",
        "comment" => $post_data, );
array_push($arr, $AssArr);
print_r($arr);

$json = json_encode($arr);// converts php associative array to json format
file_put_contents($file, $json);
echo "successfull";







所有数组都保存在一行上的json文本文件中,




all arrays saved onto json text file on one Line,

[{"name":"Video 0","comment":"3RtYLZaUUdU"},{"name":"Video 1","comment":"QrIGoya6BcE"},{"name":"Video 2","comment":"KQbr2OGydJU"},{"name":"Video 3","comment":"bad movie, hate that sh*t."},{"name":"Video 3","comment":"REMOVE THIS MOVE"},{"name":"Video 3","comment":"nooooooooooooooo"},{"name":"Video 3","comment":"BAD movie"}]





如何将它们保存在json文本文件中的单独行中,如每个数组分开来一行,感谢您的建议和帮助提前





how to save them on json text file in separate lines like each array comes in separate line, thanks for your advice and help in advance

php associative array is written on long line in json file, my task is to have these arrays to be written on separate lines in json file, please help, i tried to add linebreaks like backslash r and backslash n with double quotes and dot, nope not worked, if anyone has expereince in such problem please share your knowledge, thanks lot.




我尝试了什么:



php关联数组写得很长在json文件中,我的任务是将这些数组写在json文件中的单独行上,请帮助,我试图添加反斜杠r和反斜杠n的换行符用双引号和点,nope不起作用,如果有人有经验在这样的问题中请分享你的知识,谢谢你。



What I have tried:

php associative array is written on long line in json file, my task is to have these arrays to be written on separate lines in json file, please help, i tried to add linebreaks like backslash r and backslash n with double quotes and dot, nope not worked, if anyone has expereince in such problem please share your knowledge, thanks lot.

推荐答案

post_data =
post_data =


_POST ['data']; //用户评论输入文本
_POST['data']; //user comment input text


file =newfile.txt; //实际的json文本文件,内容高于
file = "newfile.txt"; //actual json text file, the content is above


这篇关于在json文件中的长行写的Php数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆