我有一个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']
您可以只是“ explode()”,但还不能完全达到目标.有没有更简单的解决方案,像这样?
解决方法:
好的,因此您可以看一下使用节和INI文件的数组语法的方法,例如:
[site]
social[twitter] = URL
social[facebook] = URL
social[google-plus] = URL
然后将true作为第二个参数传递:
$config = parse_ini_file(__DIR__ ."/../config.ini", true);
$array = array();
foreach($config as $path => $value) {
$temp = &$array;
foreach(explode('.', $path) as $key) {
$temp =& $temp[$key];
}
$temp = $value;
}
$config = $array;
print_r($config);
标签:ini,config,arrays,php
来源: https://codeday.me/bug/20191027/1942994.html