Saving arrays or interpreting Strings as PHP?
Posted: Sat Dec 05, 2009 7:11 pm
I'm writing a PHP application that requires the ability to take an array, save it in a file, and restore the array later. Can this be easily done?
The way I've been trying to do it is converting the Array to a String, saving that String to a file, then reversing the process until I have the original Array.
I've found the var_export function, which converts an array to a String, formatted in such a way that one could re-declare an array with that String.
For example:
<?php
$a = array (1, 2, array ("a", "b", "c"));
$b = var_export($a, true);
?>
$b now contains:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
and I want to be able to recreate $a by interpreting $b as PHP code. Can this be done?
Thanks for any and all help!
The way I've been trying to do it is converting the Array to a String, saving that String to a file, then reversing the process until I have the original Array.
I've found the var_export function, which converts an array to a String, formatted in such a way that one could re-declare an array with that String.
For example:
<?php
$a = array (1, 2, array ("a", "b", "c"));
$b = var_export($a, true);
?>
$b now contains:
array (
0 => 1,
1 => 2,
2 =>
array (
0 => 'a',
1 => 'b',
2 => 'c',
),
)
and I want to be able to recreate $a by interpreting $b as PHP code. Can this be done?
Thanks for any and all help!