数组对象 [英] Array to object

查看:84
本文介绍了数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义的框架。它使用的.ini类型的配置文件的。

I wrote a custom framework. Which uses the .ini type of config file.

我说,如果我有在.ini文件中指定为

Say, if I have a constant assigned in the .ini file as

[production]
db.mongo.hostName = localhost

我目前通过 parse_ini_file解析这个()功能。

其结果将是

$config[production][db.mongo.hostname] = "localhost";

现在我想要把这个数组的对象,它应该像

Now I wanna turn this array to an object which should go like

$config->db->mongo->hostname

我试着用它爆炸。,但我坚持这形成作为上述迭代对象。

I tried exploding it with '.', but then I am stuck with forming this as an above iterative object.

有人可以帮我这个问题。

Can someone please help me with this.

推荐答案

以下是具有单一功能导入INI数组一个基本类为您所指定的:

The following is a rudimentary class that has a single function to import the ini array as you have specified it:

/**
 * Config "Class"
 * @link http://stackoverflow.com/q/11188563/367456
 */
class Config
{
    public function __construct(array $array = array())
    {
        $this->importIniArray($array);
    }

    public function importIniArray(array $array)
    {
        foreach ($array as $key => $value) {
            $rv = &$this;
            foreach (explode('.', $key) as $pk) {
                isset($rv->$pk) || $rv->$pk = new stdClass;
                $rv = &$rv->$pk;
            }
            $rv = $value;
        }
    }
}

我只加了 __构建功能,因为你重复使用的变量。用法:

I only added the __construct function because you re-use the variable. Usage:

$config = parse_ini_file($path);

$config = new Config($config['production']);

print_r($config);

典型的输出:

Config Object
(
    [db] => stdClass Object
        (
            [mongo] => stdClass Object
                (
                    [hostname] => localhost
                    [user] => root
                )

        )

)


编辑:您也可以解决在访问的时间来定位数组成员的问题。我编译的行为类似于一个小例子,它爆炸什么。我把它叫做 DynConfig 才行,你会看到它是动态的(或者魔法):


You can also solve the problem to locate the array member at time of access. I compiled a little example that behaves similar and it explodes nothing. I called it DynConfig because as you'll see it's dynamic (or magic):

$config = new DynConfig($config['production']);
var_dump($config);
echo $config->db->mongo->hostname; # localhost

的var_dump 显示,数组只是$ P $内部pserved:

The var_dump shows that the array is just preserved internally:

object(DynConfig)#1 (1) {
  ["array":"DynConfig":private]=>
  array(2) {
    ["db.mongo.hostname"]=>
    string(9) "localhost"
    ["db.mongo.user"]=>
    string(4) "root"
  }
}

那么如何工作的呢?每次访问属性,要么存在一个关键的的的preFIX被延长,并返回同一个对象。这是在 __ GET 功能:

So how does this work? Each time you access a property, either a key exists or the prefix is extended and the same object is returned. That's in the __get function:

/**
 * Config "Class"
 * @link http://stackoverflow.com/q/11188563/367456
 */
class DynConfig
{
    private $array;

    public function __construct($array)
    {
        $this->array = $array;
    }

    public function __get($name)
    {
        static $prefix = '';
        $k = $prefix .= $name;
        if (isset($this->array[$k])) {
            $prefix = '';
            return $this->array[$k];
        }
        $prefix .= '.';
        return $this;
    }
}

然而,这是实验性的。第一个建议是更直接和更容易对付。请记住,你的配置对象应该是非常简单,它只是需要存储一些值,仅此而已。

However, this is of experimental nature. The first suggestion is much more direct and much easier to deal with. Keep in mind that your config object should be really simple, it just needs to store some values and that's it.

这篇关于数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆