的PHP文件中包含的代码,如:解析阵列
<?php
return array(
// commments...
'some_item' => 'abc',
// commments...
'some_other_item' => array(1, 2, 3),
...
);
什么是从我的PHP应用程序中,以“解析”这个文件不知何故,并能够在其更新数据的最佳方式,而不会打破格式化,代码等?
的PHP文件中包含的代码,如:解析阵列
<?php
return array(
// commments...
'some_item' => 'abc',
// commments...
'some_other_item' => array(1, 2, 3),
...
);
什么是从我的PHP应用程序中,以“解析”这个文件不知何故,并能够在其更新数据的最佳方式,而不会打破格式化,代码等?
$content = require($file);
将获得该文件的内容(注意相对路径和require
VS include
语义)。
file_put_contents($file, '<?php return ' . var_export($content, true) . ';');
会将内容写回文件,但格式会改变。如果你确实需要保持格式化,你可以看看PHP Beautifier。这不是一个完美的解决方案。
简单地include()
该文件:
$my_array = include 'myarray.php';
参见实施例#4在http://php.net/manual/en/function.include.php和http://php.net/manual/en/function.return.php
更新数组怎么办? – miki 2012-03-06 20:07:43
你想加载包含在数组中的文件名吗?如果是这样的:
$array_of_files = array('header.php','database.php','../inc/other_stuff.php');
foreach($array_of_files as $file)
{
include $file;
}
假设你的数组只有两个项目
<?php
$myArray=include 'data.php';
$myArray[count($myArray)]='Item-3';
echo $myArray[2]; // Item-3
?>
为什么你会在PHP代码数据存储在首位? – miki 2012-03-06 20:01:18
这是真正的基础数据,应用程序需要在启动时(db连接)。 – Alex 2012-03-06 20:02:51
然后首先包含文件并为数组命名,如$ myArray = Array(...);并且不需要返回它。 – 2012-03-06 20:04:15