我有一个config.ini
文件,其中包含许多这些类型的变量:
site.social.twitter = "URL"
site.social.facebook = "URL"
site.social.google-plus = "URL"
我使用PHP的内置函数来解析INI。
$config = parse_ini_file(__DIR__ ."/../config.ini");
下一步是让这个(site.social.twitter
)成为一个数组。
$config['site']['social']['twitter']
你可以'爆炸()',但它并不完全到达那里。这样的事情有没有更简单的解决方案?
好的,您可以查看使用INI文件的节和数组语法,例如:
[site]
social[twitter] = URL
social[facebook] = URL
social[google-plus] = URL
然后通过true
作为第二个参数:
$config = parse_ini_file(__DIR__ ."/../config.ini", true);
或者使用现有的INI结构构建(改编自How to access and manipulate multi-dimensional array by key names / path?):
$array = array();
foreach($config as $path => $value) {
$temp = &$array;
foreach(explode('.', $path) as $key) {
$temp =& $temp[$key];
}
$temp = $value;
}
$config = $array;
print_r($config);
简单地引用$config['site']['social']
可以获得你想要的数组吗?根据documentation,这应该产生:
Array
(
[twitter] => "URL"
[facebook] => "URL"
[google-plus] => "URL"
)