Nested arrays in ini file

I am trying to create a structure of nested arrays inside the ini settings file. I have a structure:

stuct1[123][a] = "1"
stuct1[123][b] = "2"
stuct1[123][c] = "3"
stuct1[123][d] = "4"

But that does not work. Can anyone explain if this type of structure is possible withparse_ini_file

If possible, what am I doing wrong?

+5
source share
4 answers

INI files are quite limited, and parse_ini_file is far from ideal. If you have such requirements, you'd better look for a different syntax.

What about json ? PHP support has almost the same comfort:

$data = json_decode(file_get_contents($filename), TRUE);
file_put_contents($filename, json_encode($data));
+2
source

You can use a section for this task parse_ini_file.

Be sure to set the second parameter true:

parse_ini_file("sample.ini", true);

, :

[123]
setting[] = "1"
setting[] = "2"
setting[] = "3"
setting[] = "4"

, thos

[123][setting][0] => "1"
[123][setting][1] => "2"
[123][setting][2] => "3"
[123][setting][3] => "4"
+28

. , , , .

<?php

define('BIRD', 'Dodo bird');

$ini_array = parse_ini_file("sample.ini", true);
echo('<pre>'.print_r($ini_array,true).'</pre>');

?>

parse_ini_file.ini

; This is a sample configuration file
; Comments start with ';', as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
second_section[one]="1 associated"
second_section[two]="2 associated"
second_section[]="1 unassociated"
second_section[]="2 unassociated"

[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"

Output

Array
(
    [first_section] => Array
        (
            [one] => 1
            [five] => 5
            [animal] => Dodo bird
        )

    [second_section] => Array
        (
            [path] => /usr/local/bin
            [URL] => http://www.example.com/~username
            [second_section] => Array
                (
                    [one] => 1 associated
                    [two] => 2 associated
                    [0] => 1 unassociated
                    [1] => 2 unassociated
                )

        )

    [third_section] => Array
        (
            [phpversion] => Array
                (
                    [0] => 5.0
                    [1] => 5.1
                    [2] => 5.2
                    [3] => 5.3
                )

        )

)
+9
source

Here is another way to group values ​​in ini:

my.ini:

[singles] 
test = a test 
test2 = another test 
test3 = this is a test too

[multiples] 
tests[] = a test 
tests[] = another 
test tests[] = this is a test too

my.php:

Same as:

<?php 
$init['test'] = 'a test';
$init['test2'] = 'another test';
$init['test3'] = 'this is a test too';
$init['tests'][0] = 'a test';
$init['tests'][1] = 'another test';
$init['tests'][2] = 'this is a
test too'; ?>

This works with bool set to true, can be useful with loops. Works with bool set to true.

http://php.net/manual/en/function.parse-ini-file.php

Submitted by david dot dyess on gmail dot com 4 years ago

0
source

All Articles