0

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
        )

)
4
  • Why not just use JSON instead? Commented Jul 7, 2021 at 21:58
  • @ADyson Yea, that is what I am doing currently, but I like the simplicity of a .ini file so want to see if there is a way to get a multidimensional array while using it. Commented Jul 7, 2021 at 22:04
  • You find JSON complicated then? But you're trying to work with a more complicated set of data, so a storage format which can trivially accommodate that makes more sense. IMHO the ini format in your answer below is harder to read (for humans I mean). But glad you found yourself an answer. Commented Jul 7, 2021 at 22:44
  • Well that's why im using this for a small simple set of data so it can easily be edited, added to, and/or deleted. JSON is more strict on format unlike ini, making that process a bit more tedious for me to go into and edit if need be. For larger data sets of course JSON is the better solution, just not for the 3 to 4 variables that will be changed once in a while. Commented Jul 7, 2021 at 23:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.