php parse_ini_file oop & deep

前端 未结 3 1689
囚心锁ツ
囚心锁ツ 2020-12-20 03:47

I would like to make use of somehting like [parse_ini_file][1].

Lets say for instance I have a boot.ini file that I will load for further procedure:



        
3条回答
  •  盖世英雄少女心
    2020-12-20 04:49

    $ini_array = parse_ini_file("sample.ini");
    
    $ini = new stdclass;
    foreach ($ini_array as $key => $value) {
        $last = substr(strrchr($key, '.'), 1);
        if (!$last) $last = $key;
    
        $node = $ini;
    
        foreach (explode('.', $key) as $key2) {
            if (!isset($node->$key2)) {
                $node->$key2 = new stdclass;
            }
    
            if ($key2 == $last) {
                $node->$key2 = $value;
            } else {
                $node = $node->$key2;
            }
        }
    
    }
    
    var_dump($ini->db->host1->type);
    

提交回复
热议问题