JSON-encode your array using json_encode()
and store it to a file:
$data = json_encode($_POST);
file_put_contents('file.txt', $data);
When you want to retrieve the contents, you can use file_get_contents()
or similar and then use json_decode()
to decode the JSON-string back into an array / object.
Using JSON makes the interchanging of data easier, and you don't need to parse the text file to retrieve information out of it. Just use json_decode()
and be done with it. However, I suggest you use a database instead of a plain text file to store this information, as it gives you more control.
As @Wrikken noted in the comments below, you could use the FILE_APPEND
flag - it allows you to append the data to the file instead of overwriting it.
file_put_contents('file.txt', $data, FILE_APPEND);