Here are two ways:
(1) Write the JSON representation of the array object in the file.
$arr = array( [...] ); file_put_contents( 'data.txt', json_encode( $arr ) );
Then later ...
$data = file_get_contents( 'data.txt' ); $arr = json_decode( $data );
(2) Write the serialized representation of the array object to a file.
$arr = array( [...] ); file_put_contents( 'data.txt', serialize( $arr ) );
Then later ...
$data = file_get_contents( 'data.txt' ); $arr = unserialize( $data );
I prefer the JSON method because it does not get corrupted as easily as it is serialized. You can open the data file and make changes to the contents, and it will encode / decode back without big headaches. Serialized data cannot be modified or damaged, or unserialize () will not work.
source share