php parse_ini_file oop & deep

前端 未结 3 1691
囚心锁ツ
囚心锁ツ 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:30

    Well, you need to postprocess the parse_ini_file result array then.

    $ini_array = parse_ini_file("bootstrap.ini");
    
    $ini = new stdclass;
    foreach ($ini_array as $key=>$value) {
        $c = $ini;
        foreach (explode(".", $key) as $key) {
            if (!isset($c->$key)) {
                $c->$key = new stdclass;
            }
            $prev = $c;
            $c = $c->$key;
        }
        $prev->$key = $value;
    }
    

    Update Hackety-Hack. Now using an extra $prev to unset the last object level again. (A for loop to detect the last $key would have worked better).

    If you want to use the array syntax and the object syntax, then replace the new stdclass with new ArrayObject(array(), 2);.

提交回复
热议问题