Justin Justin - 2 months ago 10x
Ini Question

PHP Config.ini file parsing with multi-level arrays

I have a

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 (
to become an array.


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


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

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 (I was bored):

$array = array();

foreach($config as $path => $value) {
    $temp = &$array;
    foreach(explode('.', $path) as $key) {
        $temp =& $temp[$key];
    $temp = $value;
$config = $array;