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?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Justin
  • 2,502
  • 7
  • 42
  • 77
  • 1
    "Doesn't quite get there"? Can you explain what you are getting? And what you expect to get, given an example configuration? – Nathaniel Ford Mar 10 '16 at 20:46
  • Can you change the ini file? Would be cleaner. – AbraCadaver Mar 10 '16 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. – Mark Manning Mar 10 '16 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. – Justin Mar 10 '16 at 21:14

2 Answers2

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);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
-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"
    )
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • It doesn't, beucase the reference is, site.social – Justin Mar 10 '16 at 21:15
  • The reference of what? What are you trying to reference? What result do you expect? What are you actually getting? – Nathaniel Ford Mar 10 '16 at 21:17
  • 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'] – Justin Mar 10 '16 at 21:20
  • 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](http://framework.zend.com/manual/1.12/en/zend.config.adapters.ini.html) – Nathaniel Ford Mar 10 '16 at 22:08