file put contents with array

You want to serialize() the array on writting, and unserialize() after reading the file.

$array = array('foo' => 'bar');
file_put_contents('foo.txt', serialize($array));
$array = unserialize(file_get_contents('foo.txt')));

Oh, and I really don't now how you echo'd your array, but echo array('foo' => 'bar'); will always print Array.


Or use print_r if you want to return a formatted array that is more easily readable in the txt file

If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to TRUE, print_r() will return the information rather than print it.

Code is:

$txt = "<pre>".print_r($data, true)."</pre>";
file_put_contents('file.txt', $txt);

Tags:

Php