php ini文件中的嵌套数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14614866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:38:28  来源:igfitidea点击:

Nested arrays in ini file

php

提问by Marty Wallace

I am trying to have a nested array structure inside an ini settings file. The structure i have is:

我试图在 ini 设置文件中有一个嵌套的数组结构。我的结构是:

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

But this doesnt work. Can anyone explain if this type of structure is possible with parse_ini_file

但这不起作用。任何人都可以解释这种类型的结构是否可行parse_ini_file

If it is possible, what am i doing wrong?

如果可能,我做错了什么?

采纳答案by Josef Kufner

INI files are pretty limited and parse_ini_file is far from perfect. If you have requirements like this, you should better look for some other syntax.

INI 文件非常有限, parse_ini_file 远非完美。如果你有这样的要求,你最好寻找一些其他的语法。

What about JSON? It's support in PHPcomes with almost same comfort:

什么JSON?它在 PHP 中支持带来了几乎相同的舒适度:

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

回答by dan-lee

You can use the sections featureof parse_ini_filefor this task.

您可以使用 的部分功能parse_ini_file完成此任务。

Be sure to set your second parameter to true:

请务必将您的第二个参数设置为true

parse_ini_file("sample.ini", true);

It's not exactly possible to make sub sections but you can make an indexed sub array like this:

制作子部分是不可能的,但您可以制作一个索引子数组,如下所示:

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

Parsed it would look similar like thos

解析它看起来像 thos

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

回答by user1032531

You can create a minimum of three levels. Maybe more, but I don't know how to do so.

您至少可以创建三个级别。也许更多,但我不知道该怎么做。

<?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

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
                )

        )

)

回答by Soroush Falahati

Here is another way to group values in the 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:

The 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 the bool set to true also, can be useful with loops. Works with the bool set to true as well.

这是对 ini 中的值进行分组的另一种方法:

我的.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

我的.php:

等同于:

<?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'; ?>

这也适用于 bool 设置为 true,对循环很有用。也适用于 bool 设置为 true。

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

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

Posted by david dot dyess at gmail dot com 4 years ago

由 david dot dyess 在 gmail dot com 发表 4 年前