.php文件包含以下代码:
<?php
return array(
// commments...
'some_item' => 'abc',
// commments...
'some_other_item' => array(1, 2, 3),
...
);
Run Code Online (Sandbox Code Playgroud)
从我的PHP应用程序中以某种方式"解析"此文件的最佳方法是什么,并且能够更新其中的数据,而不会破坏格式,代码等?
简单include()的文件:
$my_array = include 'myarray.php';
Run Code Online (Sandbox Code Playgroud)
请参阅http://php.net/manual/en/function.include.php和http://php.net/manual/en/function.return.php 中的示例 #4
$content = require($file);将获取文件的内容(注意相对路径和requirevs include语义).
file_put_contents($file, '<?php return ' . var_export($content, true) . ';');将内容写回文件,但格式将更改.如果您确实需要保留格式,可以查看PHP Beautifier.虽然这不是一个完美的解决方案.