Page 1 of 1
parse_ini_file, array with more than two dimensions?
Posted: Wed Mar 17, 2004 8:42 pm
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
Posted: Wed Mar 17, 2004 8:52 pm
by markl999
You can do this with
PEAR's Config but not with parse_ini_file as far as i know.
Posted: Wed Mar 17, 2004 9:06 pm
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
)
)
)
Posted: Wed Mar 17, 2004 10:11 pm
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
(
їxhtml] => Array
(
їclassName] => FormatEngineXHTML
їclassFile] => FormatEngineXHTML.class.php
)
їwrapperManager] => Array
(
їWrapperManager] => Array
(
їclassName] => WrapperManager
їclassFile] => WrapperManager.class.php
)
їGiantMonkeyRobot] => Array
(
їclassName] => GiantMonkeyRobot
їclassFile] => GiantMonkeyRobot.class.php
)
)
їdefaultWrapper] => Array
(
їdefaultWrapper] => 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
Posted: Thu Mar 18, 2004 5:42 am
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).
