I've used variable variables before but for some reason I can't figure this
snippet out. Why doesn't $ini_file get set (or appended to). Is there a
better more elegant solution to parsing these keys and splitting them into
sub-array keys as you see what I'm trying to do. I was thinking some
recursion, but it seems overkill for such a simple task.

Given an .ini file like so:

[production]
agis_core.adapter               = Mysqli
agis_core.params.host           = 10.10.10.46
agis_core.params.username       = USERNAME
agis_core.params.password       = PASSWORD
agis_core.params.dbname         = agis_core
agis_core.params.port           = 3306

I'm writing a simple parser (to be semi-compatible with the Zend Framework
one apparently but without the bloat of ZF)

require_once $path.'/../classes/IniParser.class.php';
try
{
        $config_ini = new IniParser($path.'/../config.ini', true, true);
}
catch (Exception $e)
{
        echo 'Caught Exception parsing ini
file.<br>'.$e->getMessage()."\n";
}


class IniParser
{
        private $file;
        public  $ini_array;

        function __construct($file, $process_sections=true, $explode=false)
        {
                $this->file = $file;
                $this->ini_array = parse_ini_file($file,$process_sections);
                if (!$this->ini_array)
                {
                        //we only check this if we failed since Disk I/O is
expensive
                        if (!file_exists($file)) throw new Exception('File
Not Found: '.$file);
                }

                if ($explode) $this->explode_ini();
        }

        public function explode_ini()
        {
                $ini_array = array();

                foreach($this->ini_array as $heading => $key_vals)
                {
                        foreach ($key_vals as $k => $v)
                        {
                                $path = 'ini_array[\''.$heading.'\']';
                                $subsection = explode('.', $k);
                                foreach ($subsection as $ss) $path .=
'[\''.$ss.'\']';
                                //echo $path.' = '.$v.'<br>';
                                $$path = $v;
                                var_dump($path, $$path, $ini_array);
                        }
                }

                $this->ini_array = $ini_array;
        }
}
?>

But the $ini_array is not being set?!? Here is the output I get...

string 'ini_array['production']['agis_core']['adapter']' (length=47)

string 'Mysqli' (length=6)

array
  empty

string 'ini_array['production']['agis_core']['params']['host']' (length=54)

string '10.10.10.46' (length=11)

array
  empty

string 'ini_array['production']['agis_core']['params']['username']'
(length=58)

string 'USERNAME' (length=7)

array
  empty

...


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to