PHP:使用 file() 将 config.ini 读取到数组
我的配置文件如下所示:
title = myTitle;
otherTitle = myOtherTitle;
当我使用 file() 读取文件时,它会创建这个数组
[0] => title = myTitle;
[1] => otherTitle = myOtherTitle;
,而我希望数组看起来像
[title] => myTitle;
[otherTitle] => myOtherTitle;
我是否使用了错误的方法? 我应该将整个配置读入一个刺中并从那里爆炸吗?
My config file looks like this:
title = myTitle;
otherTitle = myOtherTitle;
when I read the file with file(), it creates this array
[0] => title = myTitle;
[1] => otherTitle = myOtherTitle;
and what I want the array to look like is
[title] => myTitle;
[otherTitle] => myOtherTitle;
Am I using the wrong approach her? Should i just read the entire config into a sting and explode it from there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
parse_ini_file
函数。 它在 PHP 4 和 5 中可用。如果您的配置文件如下所示:
该函数将返回以下关联数组:
You can use the
parse_ini_file
function. It's available in PHP 4 and 5.If your config file looks like this:
The function will return the following associative array:
我只是循环遍历文件并单独分解每一行。 这是一个简单的示例,之后您将得到
$config
根据请求保存所有数据。I would just loop through the file and explode each line individually. Here's a simple example, after which you'll end up with
$config
holding all of your data as requested.