将数组转换为.ini文件

我需要将.ini文件解析为数组,然后更改数组的值并将其导出到相同的.ini文件。 我设法读取文件,但没有找到任何简单的方法来回写。 有什么建议么?

示例.ini文件:

1 = 0;
2 = 1372240157;    // timestamp.

为了写入.ini文件,您需要创建自己的函数,因为除了阅读外,PHP不提供任何开箱即用的功能(可以在这里找到它:http://php.net/manual/pl/ function.parse-INI-file.php)。

可能将多维数组封装为.ini -syntax兼容字符串的函数示例如下所示:

function arr2ini(array $a, array $parent = array())
{
    $out = '';
    foreach ($a as $k => $v)
    {
        if (is_array($v))
        {
            //subsection case
            //merge all the sections into one array...
            $sec = array_merge((array) $parent, (array) $k);
            //add section information to the output
            $out .= '[' . join('.', $sec) . ']' . PHP_EOL;
            //recursively traverse deeper
            $out .= arr2ini($v, $sec);
        }
        else
        {
            //plain key->value case
            $out .= "$k=$v" . PHP_EOL;
        }
    }
    return $out;
}

你可以像这样测试它:

$x = [
  'section1' => [
    'key1' => 'value1',
    'key2' => 'value2',
    'subsection' => [
      'subkey' => 'subvalue',
      'further' => ['a' => 5],
      'further2' => ['b' => -5]]]];
echo arr2ini($x);

(请注意,从PHP 5.4开始,只能使用短阵列语法。)

还请注意,它不保留您的问题中存在的评论 。 当它是用于更新文件的软件(而不是人)时,没有简单的方法来记住它们。


我对rr提供的功能做了重大改动(很多感谢启动!)

我对该版本中处理多维属性的方式感到不满。 我从php文档页面获取了parse_ini_file的示例ini文件,并得到了包含键third_section.phpversionthird_section.urls - 不是我所期望的。

我尝试使用RecursiveArrayIterator进行无限嵌套,但不幸的是,在其下面具有键值对的头部是parse_ini_string在窒息出错消息之前将处理的递归的最大限制。

所以我从零开始,添加了一些曲线球作为第四个也是最后一个项目,并最终得出了这个结论:

$test = array(
    'first_section' => array(
        'one' => 1,
        'five' => 5,
        'animal' => "Dodo bird",
    ),
    'second_section' => array(
        'path' => "/usr/local/bin",
        'URL' => "http://www.example.com/username",
    ),

    'third_section' => array(
        'phpversion' => array(5.0, 5.1, 5.2, 5.3),
        'urls' => array(
            'svn' => "http://svn.php.net",
            'git' => "http://git.php.net",
        ),
    ),

    'fourth_section' => array(
        7.0, 7.1, 7.2, 7.3,
    ),
    'last_item' => 23,
);
echo '<pre>';
print_r($test);
echo '<hr>';
$ini = build_ini_string($test);
echo $ini;
echo '<hr>';
print_r( parse_ini_string($ini, true) );

function build_ini_string(array $a) {
    $out = '';
    $sectionless = '';
    foreach($a as $rootkey => $rootvalue){
        if(is_array($rootvalue)){
            // find out if the root-level item is an indexed or associative array
            $indexed_root = array_keys($rootvalue) == range(0, count($rootvalue) - 1);
            // associative arrays at the root level have a section heading
            if(!$indexed_root) $out .= PHP_EOL."[$rootkey]".PHP_EOL;
            // loop through items under a section heading
            foreach($rootvalue as $key => $value){
                if(is_array($value)){
                    // indexed arrays under a section heading will have their key omitted
                    $indexed_item = array_keys($value) == range(0, count($value) - 1);
                    foreach($value as $subkey=>$subvalue){
                        // omit subkey for indexed arrays
                        if($indexed_item) $subkey = "";
                        // add this line under the section heading
                        $out .= "{$key}[$subkey] = $subvalue" . PHP_EOL;
                    }
                }else{
                    if($indexed_root){
                        // root level indexed array becomes sectionless
                        $sectionless .= "{$rootkey}[] = $value" . PHP_EOL;
                    }else{
                        // plain values within root level sections
                        $out .= "$key = $value" . PHP_EOL;
                    }
                }
            }

        }else{
            // root level sectionless values
            $sectionless .= "$rootkey = $rootvalue" . PHP_EOL;
        }
    }
    return $sectionless.$out;
}

我的输入和输出数组匹配(功能上,无论如何),我的ini文件看起来像这样:

fourth_section[] = 7
fourth_section[] = 7.1
fourth_section[] = 7.2
fourth_section[] = 7.3
last_item = 23

[first_section]
one = 1
five = 5
animal = Dodo bird

[second_section]
path = /usr/local/bin
URL = http://www.example.com/username

[third_section]
phpversion[] = 5
phpversion[] = 5.1
phpversion[] = 5.2
phpversion[] = 5.3
urls[svn] = http://svn.php.net
urls[git] = http://git.php.net

我知道这可能有点矫枉过正,但我​​真的需要在我自己的两个项目中使用这个功能。 现在我可以读取一个ini文件,进行更改并保存。


RR的答案是有效的,我添加了一项改变

在其他语句中

//plain key->value case
$out .= "$k=$v" . PHP_EOL;

将其更改为

//plain key->value case
$out .= "$k="$v"" . PHP_EOL;

通过“在值附近”,您可以在INI中拥有很大的值,否则parse_ini_ *函数会有问题

http://missioncriticallabs.com/blog/2009/08/double-quotation-marks-in-php-ini-files/

链接地址: http://www.djcxy.com/p/14489.html

上一篇: Convert array to an .ini file

下一篇: ^char type hint not permitted for clojure defn parameter