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.
json_encode()
andjson_encode()
if you really want to save the array / object to a file? Orserialize()
andunserialize()
.var_export()
comes closes to what you want, but there's no built-in PHP function that'd export a structure as executable PHP code. Useserialize()
orjson_encode()
to store the data.