Convert PHP array string into an array
I have an array:
$myArray = array('key1'=>'value1', 'key2'=>'value2');
I save it as a variable:
$fileContents = var_dump($myArray);
How can convert the variable back to use as a regular array?
echo $fileContents[0]; //output: value1
echo $fileContents[1]; //output: value2
I think you might want to look into serialize
and unserialize
.
$myArray = array('key1'=>'value1', 'key2'=>'value2');
$serialized = serialize($myArray);
$myNewArray = unserialize($serialized);
print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )
serialize might be the right answer - but I prefer using JSON - human editing of the data will be possible that way...
$myArray = array('key1'=>'value1', 'key2'=>'value2');
$serialized = json_encode($myArray);
$myNewArray = json_decode($serialized, true);
print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )