我想避免写入DB并使用常量/数组来处理lang文件等.
即:
$lang = array (
'hello' => 'hello world!'
);
Run Code Online (Sandbox Code Playgroud)
并能够从后台编辑它.(然后我不会从穷人的数据库中取出它,而是使用$ lang ['hello'] ..).
您有什么建议以最好和最有效的方式拉动它?
Jan*_*ser 17
我找到的最有效的方式是这样的:
以某种方式在php中构建你的数组并使用它将其导出到一个文件中 var_export()
file_put_contents( '/some/file/data.php', '<?php return '.var_export( $data_array, true ).";\n" );
Run Code Online (Sandbox Code Playgroud)
然后,无论你需要什么,这个数据就像这样拉
$data = include '/some/file/data.php';
Run Code Online (Sandbox Code Playgroud)
Oli*_*sin 12
绝对是JSON
要保存它:
file_put_contents("my_array.json", json_encode($array));
Run Code Online (Sandbox Code Playgroud)
要取回它:
$array = json_decode(file_get_contents("my_array.json"));
Run Code Online (Sandbox Code Playgroud)
就如此容易 !
好吧,如果你坚持把数据转换成文件,你可以考虑PHP函数serialize()和unserialize(),然后使用数据放入文件file_put_contents.
例:
<?php
$somearray = array( 'fruit' => array('pear', 'apple', 'sony') );
file_put_contents('somearray.dat', serialize( $somearray ) );
$loaded = unserialize( file_get_contents('somearray.dat') );
print_r($loaded);
?>
Run Code Online (Sandbox Code Playgroud)