PHP中的INI文件到多维数组

发布于 2024-12-06 00:43:17 字数 423 浏览 2 评论 0原文

我有下一个 INI 文件:

a.b.c = 1
a.b.d.e = 2

我正在使用 parse_ini_file 解析该文件。它返回:

array(
    'a.b.c' => 1,
    'a.b.d.e' => 2
)

但我想创建一个多维数组。我的输出应该是:

array(
    'a' => array(
        'b' => array(
            'c' => 1,
            'd' => array(
                'e' => 2
            )
        )
    )
)

提前谢谢你。

I have the next INI file:

a.b.c = 1
a.b.d.e = 2

I am parsing this file using parse_ini_file. And it returns:

array(
    'a.b.c' => 1,
    'a.b.d.e' => 2
)

But I want to create a multidimensional array. My outout should be:

array(
    'a' => array(
        'b' => array(
            'c' => 1,
            'd' => array(
                'e' => 2
            )
        )
    )
)

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南城旧梦 2024-12-13 00:43:17

我是这样看的:

<?php

class ParseIniMulti {

    public static function parse($filename) {
        $ini_arr = parse_ini_file($filename);
        if ($ini_arr === FALSE) {
            return FALSE;
        }
        self::fix_ini_multi(&$ini_arr);
        return $ini_arr;
    }

    private static function fix_ini_multi(&$ini_arr) {
        foreach ($ini_arr AS $key => &$value) {
            if (is_array($value)) {
                self::fix_ini_multi($value);
            }
            if (strpos($key, '.') !== FALSE) {
                $key_arr = explode('.', $key);
                $last_key = array_pop($key_arr);
                $cur_elem = &$ini_arr;
                foreach ($key_arr AS $key_step) {
                    if (!isset($cur_elem[$key_step])) {
                        $cur_elem[$key_step] = array();
                    }
                    $cur_elem = &$cur_elem[$key_step];
                }
                $cur_elem[$last_key] = $value;
                unset($ini_arr[$key]);
            }
        }
    }

}


var_dump(ParseIniMulti::parse('test.ini'));

This is how I see it:

<?php

class ParseIniMulti {

    public static function parse($filename) {
        $ini_arr = parse_ini_file($filename);
        if ($ini_arr === FALSE) {
            return FALSE;
        }
        self::fix_ini_multi(&$ini_arr);
        return $ini_arr;
    }

    private static function fix_ini_multi(&$ini_arr) {
        foreach ($ini_arr AS $key => &$value) {
            if (is_array($value)) {
                self::fix_ini_multi($value);
            }
            if (strpos($key, '.') !== FALSE) {
                $key_arr = explode('.', $key);
                $last_key = array_pop($key_arr);
                $cur_elem = &$ini_arr;
                foreach ($key_arr AS $key_step) {
                    if (!isset($cur_elem[$key_step])) {
                        $cur_elem[$key_step] = array();
                    }
                    $cur_elem = &$cur_elem[$key_step];
                }
                $cur_elem[$last_key] = $value;
                unset($ini_arr[$key]);
            }
        }
    }

}


var_dump(ParseIniMulti::parse('test.ini'));
丢了幸福的猪 2024-12-13 00:43:17

实际上很简单,您只需通过分解其键来更改已有数组的格式:

$ini_preparsed = array(
    'a.b.c' => 1,
    'a.b.d.e' => 2
);


$ini = array();
foreach($ini_preparsed as $key => $value)
{
    $p = &$ini;
    foreach(explode('.', $key) as $k)
        $p = &$p[$k];
    $p = $value;
}
unset($p);

print_r($ini);

输出:

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => 1
                    [d] => Array
                        (
                            [e] => 2
                        )

                )

        )

)

另请参阅: String将数组结构转换为Array

It's actually quite simple, you only need to change the format of the array you already have by exploding it's key:

$ini_preparsed = array(
    'a.b.c' => 1,
    'a.b.d.e' => 2
);


$ini = array();
foreach($ini_preparsed as $key => $value)
{
    $p = &$ini;
    foreach(explode('.', $key) as $k)
        $p = &$p[$k];
    $p = $value;
}
unset($p);

print_r($ini);

Output:

Array
(
    [a] => Array
        (
            [b] => Array
                (
                    [c] => 1
                    [d] => Array
                        (
                            [e] => 2
                        )

                )

        )

)

See as well: String with array structure to Array.

输什么也不输骨气 2024-12-13 00:43:17

查看 Zend_Config_Ini 类。它可以完成您想要的操作,您可以独立使用它(无需 Zend Framework 的其余部分),并且作为奖励,它支持节继承。

使用 toArray 方法,您可以从配置对象创建一个数组。

Have a look at the Zend_Config_Ini class. It does what you want, you can use it standalone (without the rest of Zend Framework) and as a bonus it supports section inheritance.

With the toArray method you can create an array from the config object.

薯片软お妹 2024-12-13 00:43:17

看看 PHProp

Zend_Config_Ini 类似,但您可以在配置中引用一个键,例如 ${key}

Take a look at PHProp.

Similar to Zend_Config_Ini, but you can refer to a key in your config like ${key}

白衬杉格子梦 2024-12-13 00:43:17

这是我的类,用于将配置 ini 文件解析为多维数组:

class Cubique_Config {
    const SEPARATOR = '.';

    private static $_data = null;

    public static function get() {
        if (is_null(self::$_data)) {
            $commonIniFile = APP . '/config' . '/common.ini';
            $envIniFile = APP . '/config' . '/' . ENV . '.ini';
            if (!file_exists($commonIniFile)) {
                throw new Exception('\'' . $commonIniFile . '\' config file not found');
            }
            if (!file_exists($envIniFile)) {
                throw new Exception('\'' . $envIniFile . '\' config file not found');
            }
            $commonIni = parse_ini_file($commonIniFile);
            $envIni = parse_ini_file($envIniFile);
            $mergedIni = array_merge($commonIni, $envIni);
            self::$_data = array();
            foreach ($mergedIni as $rowKey => $rowValue) {
                $explodedRow = explode(self::SEPARATOR, $rowKey);
                self::$_data = array_merge_recursive(self::$_data, self::_subArray($explodedRow, $rowValue));
            }
        }
        return self::$_data;
    }

    private static function _subArray($explodedRow, $value) {
        $result = null;
        $explodedRow = array_values($explodedRow);
        if (count($explodedRow)) {
            $firstItem = $explodedRow[0];
            unset($explodedRow[0]);
            $result[$firstItem] = self::_subArray($explodedRow, $value);
        } else {
            $result = $value;
        }
        return $result;
    }
}

It's a my class for parsing config ini files to a multidimensional array:

class Cubique_Config {
    const SEPARATOR = '.';

    private static $_data = null;

    public static function get() {
        if (is_null(self::$_data)) {
            $commonIniFile = APP . '/config' . '/common.ini';
            $envIniFile = APP . '/config' . '/' . ENV . '.ini';
            if (!file_exists($commonIniFile)) {
                throw new Exception('\'' . $commonIniFile . '\' config file not found');
            }
            if (!file_exists($envIniFile)) {
                throw new Exception('\'' . $envIniFile . '\' config file not found');
            }
            $commonIni = parse_ini_file($commonIniFile);
            $envIni = parse_ini_file($envIniFile);
            $mergedIni = array_merge($commonIni, $envIni);
            self::$_data = array();
            foreach ($mergedIni as $rowKey => $rowValue) {
                $explodedRow = explode(self::SEPARATOR, $rowKey);
                self::$_data = array_merge_recursive(self::$_data, self::_subArray($explodedRow, $rowValue));
            }
        }
        return self::$_data;
    }

    private static function _subArray($explodedRow, $value) {
        $result = null;
        $explodedRow = array_values($explodedRow);
        if (count($explodedRow)) {
            $firstItem = $explodedRow[0];
            unset($explodedRow[0]);
            $result[$firstItem] = self::_subArray($explodedRow, $value);
        } else {
            $result = $value;
        }
        return $result;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文