Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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" } 
]
;



PHP
<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";



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"}]


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.


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.
Posted
Updated 27-Jan-18 23:56pm
Comments
Mohibur Rashid 28-Jan-18 16:57pm    
Why do you want to do that?
ilhomelv 28-Jan-18 17:08pm    
the array is saved on file in one long line, how to save the array in the file in separate lines, instead of having one long line of arrays. thank u

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900