Alex Rivera | Logout

Print array to a file

Asked 2010-04-13T10:34:35.253
210

I would like to print an array to a file.

I would like the file to look exactly similar like how a code like this looks.

print_r ($abc); assuming $abc is an array.

Is there any one lines solution for this rather than regular for each look.

P.S - I currently use serialize but i want to make the files readable as readability is quite hard with serialized arrays.

Edit
Report

2 Answers

31

You could try:

$h = fopen('filename.txt', 'r+');
fwrite($h, var_export($your_array, true));
answered 2010-04-13T10:38:25.637
9

You can try this, $myArray as the Array

$filename = "mylog.txt";
$text = "";
foreach($myArray as $key => $value)
{
    $text .= $key." : ".$value."\n";
}
$fh = fopen($filename, "w") or die("Could not open log file.");
fwrite($fh, $text) or die("Could not write file!");
fclose($fh);
answered 2012-10-05T06:03:27.857

Your Answer