parse_ini_file, array with more than two dimensions?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
compound_eye
Forum Newbie
Posts: 15
Joined: Wed Mar 17, 2004 8:42 pm

parse_ini_file, array with more than two dimensions?

Post by compound_eye »

if i have an ini file called "xhtml.ini"
that looks something like this

Code: Select all

їxhtml]
className = FormatEngineXHTML
classFile = "FormatEngineXHTML.class.php"

їwrapperManager]
className = WrapperManager
classFile = "WrapperManager.class.php"

їdefaultWrapper]
defaultWrapper = xhtml
then i can use:

Code: Select all

$setting =  parse_ini_file("xhtml.ini", true);

to get a 2 dimensional array of those ini file settings
so

Code: Select all

echo $settingї'wrapperManager']ї'className'];


would give:
WrapperManager
is there a way to make an ini file with more than 2 dimensions?

thanks,

Mathew
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You can do this with PEAR's Config but not with parse_ini_file as far as i know.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I might be totaly out in the blue but:

Code: Select all

$setting =  parse_ini_file("xhtml.ini",true);
foreach ($setting['wrapperManager'] as $k => $v) {
    if (ereg ("\[(.*)\]", $k, $reg)) {
        $setting['wrapperManager'][$reg[1]][] = $v;
        unset($setting['wrapperManager'][$k]);
    }
}
print_r($setting);
Just testing on one area, needs tweaking...

Source:

Code: Select all

їwrapperManager]
classNameїfoo] = WrapperManagerFoo
classFileїfoo] = "WrapperManagerFoo.class.php"
classNameїbar] = WrapperManagerBar
classFileїbar] = "WrapperManagerBar.class.php"
Result:

Code: Select all

Array
(
    їxhtml] => Array
        (
            їclassName] => FormatEngineXHTML
            їclassFile] => FormatEngineXHTML.class.php
        )

    їwrapperManager] => Array
        (
            їfoo] => Array
                (
                    ї0] => WrapperManagerFoo
                    ї1] => WrapperManagerFoo.class.php
                )

            їbar] => Array
                (
                    ї0] => WrapperManagerBar
                    ї1] => WrapperManagerBar.class.php
                )

        )

)
compound_eye
Forum Newbie
Posts: 15
Joined: Wed Mar 17, 2004 8:42 pm

Post by compound_eye »

you know i think ereg might just be the answer...

if i have the ini file:

Code: Select all

їxhtml]
className = FormatEngineXHTML
classFile = "FormatEngineXHTML.class.php"

їwrapperManager]
WrapperManagerїclassName] = WrapperManager
WrapperManagerїclassFile] = "WrapperManager.class.php"
GiantMonkeyRobotїclassName] = GiantMonkeyRobot
GiantMonkeyRobotїclassFile] = "GiantMonkeyRobot.class.php"

їdefaultWrapper]
defaultWrapper = xhtml
and the code:

Code: Select all

<?php
<?php

$setting =  parse_ini_file('test.ini',true);

foreach ($setting as $outer_key => $outer_value)
{
     foreach ($setting[$outer_key] as $key => $value)
     {
          if (ereg ("\[(.*)\]", $key, $reg))
          {
               $inner_key = substr ( $key, 0 , strpos($key,'['));
               $setting[$outer_key][$inner_key][$reg[1]] = $value;
               unset($setting[$outer_key][$key]);
          }
     }
}
print_r($setting);


?>
?>
i get:

Code: Select all

Array
(
    &#1111;xhtml] =&gt; Array
        (
            &#1111;className] =&gt; FormatEngineXHTML
            &#1111;classFile] =&gt; FormatEngineXHTML.class.php
        )

    &#1111;wrapperManager] =&gt; Array
        (
            &#1111;WrapperManager] =&gt; Array
                (
                    &#1111;className] =&gt; WrapperManager
                    &#1111;classFile] =&gt; WrapperManager.class.php
                )

            &#1111;GiantMonkeyRobot] =&gt; Array
                (
                    &#1111;className] =&gt; GiantMonkeyRobot
                    &#1111;classFile] =&gt; GiantMonkeyRobot.class.php
                )

        )

    &#1111;defaultWrapper] =&gt; Array
        (
            &#1111;defaultWrapper] =&gt; xhtml
        )

)
this seems to work,

i wonder if there is a way to make this a recursive function so that you could have an arbitrary depth to the array?

thanks very much for you help

mathew
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Not sure what you mean (english isn't really my flavour), but post an example of your ini file 'idea' of how you like it to be. Might help (me). ;)
Post Reply