Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save array to PHP file

Tags:

I have a PHP file to store an array:

<?php   $arr = array (     "A" => "one",     "B" => "two",     "C" => "three"   ); ?> 

I am using require to open the file, and each entry loads into a form. (foreach loop) I would like to save the $_POST variables back (overwriting) to the original file in the same format. I have no trouble making the form and sending back the variables. I just need a way to print the array back into the original file.

Example result:

<?php   $arr = array (     "A" => "new value",     "B" => "other new value",     "C" => "third new value"   ); ?> 

I have been unable to use print_r, as the format returned is incorrect. How can I do this successfully? Thank you.

like image 893
Mooseman Avatar asked Aug 20 '13 18:08

Mooseman


People also ask

How do I save an array to a file?

You can save your NumPy arrays to CSV files using the savetxt() function. This function takes a filename and array as arguments and saves the array into CSV format.


1 Answers

The function you're looking for is var_export

You would use it like this

file_put_contents($filename, '<?php $arr = ' . var_export($arr, true) . ';'); 

DEMO

like image 106
MosheK Avatar answered Sep 22 '22 18:09

MosheK