Search code examples
phpini

Parse a multidimensional array from .ini file


I am wondering if there is anyway to (in an .ini file) implement something that when parsed by the parse_ini_file() (in php) will create a multidimensional array instead of just an associative array?

I've been searching but have came up empty, so I was wondering if anyone over here might have an idea on whether something like this is allowed or not.

Example:

file.ini (semi-sudo-code):

input = input1 = ('Hello world this is test #1')
input = input2 = ('Hello world this is test #2')

code.php:

$array = parse_ini_file("file.ini", true);
print_r($array);

Expected output:

Array
(
    [input] => Array
        (
            [input1] => Hello world this is test #1
            [input2] => Hello world this is test #2
        )

)

Solution

  • Seems like I found my own answer after figuring out a better way to word my question.

    .ini Files can have sections that are enclosed in brackets [ ] for example:

    [input]
      input1 = ('Hello world this is test #1');
      input2 = ('Hello world this is test #2');
    

    This is exactly what I was looking for as the sections (when parsed) turn it into a multidimensional array:

    Array
    (
        [input] => Array
            (
                [input1] => Hello world this is test #1
                [input2] => Hello world this is test #2
            )
    
    )
    

    Definitely should've waited a bit longer and researched a bit more before posting this question, but I'll leave it up for others who may have the same problem as I initially did.