3

I have a config.ini file that has a number of these types of variables:

site.social.twitter = "URL"
site.social.facebook = "URL"
site.social.google-plus = "URL"

I use PHP's built-in function to parse the INI.

$config = parse_ini_file(__DIR__ ."/../config.ini");

The next step is to get this (site.social.twitter) to become an array.

$config['site']['social']['twitter']

You could just 'explode()', but it doesn't quite get there. Is there any simpler solutions to something like this?

4
  • 1
    "Doesn't quite get there"? Can you explain what you are getting? And what you expect to get, given an example configuration? Commented Mar 10, 2016 at 20:46
  • Can you change the ini file? Would be cleaner. Commented Mar 10, 2016 at 20:47
  • @AbraCadaver: You beat me to it. :-) Justin - look at the PHP documentation on parse_ini_file. You can get closer to what you want by just changing your ini file to url['site.social.twitter']. That would allow you to reference it as $config['url'] and getting the array from there. Commented Mar 10, 2016 at 20:51
  • Yeah I suppose I could change the config... somewhat of a pain, as it has already been implemented throughout the site. So I will have to re-config. One thing I did think that could actually help, is by using [sections] that will help the parsing a fair bit too. Commented Mar 10, 2016 at 21:14

2 Answers 2

5

OK, so you can look at using sections and the array syntax for INI files such as:

[site]
social[twitter] = URL
social[facebook] = URL
social[google-plus] = URL

Then pass true as the second argument:

$config = parse_ini_file(__DIR__ ."/../config.ini", true);

Or to build using your existing INI structure (adapted from How to access and manipulate multi-dimensional array by key names / path?):

$array = array();

foreach($config as $path => $value) {
    $temp = &$array;
    foreach(explode('.', $path) as $key) {
        $temp =& $temp[$key];
    }
    $temp = $value;
}
$config = $array;
print_r($config);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, gotta love options! Love it. Thanks @AbraCadaver
-1

Does simply referencing $config['site']['social'] get you the array you want? According to the documentation this should yield:

Array
    (
        [twitter] => "URL"
        [facebook] => "URL"
        [google-plus] => "URL"
    )

4 Comments

It doesn't, beucase the reference is, site.social
The reference of what? What are you trying to reference? What result do you expect? What are you actually getting?
Well, if you did, $ini = parse_ini_string("site.social.twitter"); it would still be, $ini['site.social.twitter']...... it doesn't split them up. my goal was, $ini['site']['social']['twitter']
Ah... I see. Your specification was quite unclear. You don't happen to be using a Zend framework? Then you can use Zend_Config_ini

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.