我想你可能想看看serialize
和unserialize
.
$myArray = array('key1'=>'value1', 'key2'=>'value2'); $serialized = serialize($myArray); $myNewArray = unserialize($serialized); print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )
序列化可能是正确的答案 - 但我更喜欢使用JSON - 人类编辑数据将是可能的...
$myArray = array('key1'=>'value1', 'key2'=>'value2'); $serialized = json_encode($myArray); $myNewArray = json_decode($serialized); print_r($myNewArray); // Array ( [key1] => value1 [key2] => value2 )
尝试使用var_export生成有效的PHP语法,将其写入文件,然后"包含"该文件:
$myArray = array('key1'=>'value1', 'key2'=>'value2'); $fileContents = ''; // ... after writing $fileContents to 'myFile.php' include 'myFile.php'; echo $myArray['key1']; // Output: value1