php从ini文件中解析多维数组

来源:赵克立博客 分类: PHP 标签:PHP数组PHP配置发布时间:2020-11-12 22:53:58最后更新:2020-11-12 22:57:47浏览:541
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2020-11-12 22:57:47
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

php解析ini格式的文件自带有两个函数parse_ini_file和parse_ini_string,本身是可以解析二维数组的,但是不支持二维以上,但是官方示例也给出了解析二维以上的函数如下

/**
 * 解析ini文件支持多维数组解析
 * @link https://www.php.net/manual/zh/function.parse-ini-file.php#114842
 * @param       $file
 * @param false $process_sections
 * @param int   $scanner_mode
 * @return array|false|mixed
 */
function parseIniStringMulti($iniString, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL)
{
    $explode_str = '.';
    $escape_char = "'";
    // load ini file the normal way
    $data = parse_ini_string($iniString, $process_sections, $scanner_mode);
    if (!$process_sections) {
        $data = [$data];
    }
    foreach ($data as $section_key => $section) {
        if (!is_array($section)) {
            continue;
        }
        // loop inside the section
        foreach ($section as $key => $value) {
            if (strpos($key, $explode_str)) {
                if (substr($key, 0, 1) !== $escape_char) {
                    // key has a dot. Explode on it, then parse each subkeys
                    // and set value at the right place thanks to references
                    $sub_keys = explode($explode_str, $key);
                    $subs     =& $data[$section_key];
                    foreach ($sub_keys as $sub_key) {
                        if (!isset($subs[$sub_key])) {
                            $subs[$sub_key] = [];
                        }
                        $subs =& $subs[$sub_key];
                    }
                    // set the value at the right place
                    $subs = $value;
                    // unset the dotted key, we don't need it anymore
                    unset($data[$section_key][$key]);
                }
                // we have escaped the key, so we keep dots as they are
                else {
                    $new_key                      = trim($key, $escape_char);
                    $data[$section_key][$new_key] = $value;
                    unset($data[$section_key][$key]);
                }
            }
        }
    }
    if (!$process_sections) {
        $data = $data[0];
    }
    return $data;
}

$inistr=<<<eot

[db_config]
username=admin
password=123456

[cache]
redis[host]=127.0.0.127
redis[password]=123456
redis.domain[]=www.aaa.com
redis.domain[]=www.bbb.com
redis.domain[]=www.ccc.com
file[path]=/www/data/
file[max_size]=1024

[app]
debug=true
trace=false

[multi]
foo.data.config.debug = true
foo.data.password = 123456



[normal]
foo = bar
; use quotes to keep your key as it is
'foo.with.dots' = true

[array]
foo[] = 1
foo[] = 2

[dictionary]
foo[debug] = false
foo[path] = /some/path

[multi]
foo.data.config.debug = true
foo.data.password[] = 123456
foo.data.password[] = 123456

eot;
$data=parseIniStringMulti($inistr,true);


print_r($data);

echo $data['cache']['redis']['host'].PHP_EOL;
echo $data['cache']['redis']['password'].PHP_EOL;

上面函数为了测试方便,参数传的是INI字符串,实际使用中可自行修改的parse_ini_file

2011121605193064536466.png


微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python