1

我试图在这样的类中动态地获取数据:

Class foo
{
    private $config=array();

    public __construct(){    
        $this->config['site']['title'] = 'XXXXX';
        $this->config['site']['favicon'] = 'XXXXX.ico';
        $this->config['site']['keywords'] = array('page','cute','other');        
        $this->config['otherthing'] = 'value';
        /**etc**/
    }

    public function set($name,$value)
    {
        $tmp = explode('.',$name);
        /** i dont know what i can do here **/
    }

    public function get($name)
    {
        $tmp = explode('.',$name);
        /** i dont know what i can do here **/
    }
}

$thing = new foo;
$this->set('site.keywords',array('other','keywords'));//this change a data inside foo->config['site']['keywords']

echo $this->get('site.title'); // gets data inside foo->config['site']['title']
echo $this->get('otherthing'); // gets data inside foo->config['otherthing']

数组维度可以动态更改,我想在 foo->config 中设置/检索数据,顺便调用数组:函数(fist.second.third.four.etc)。

编辑:

我可以使用explode 创建一个函数,我探索了这种可能性,但是如果我使用这样的函数:

function get($name)
{
    $tmp = explode('.',$name);
    if(isset($this->config[$tmp[0]][$tmp[1]]))
        return $this->config[$tmp[0]][$tmp[1]];
    return '';
}

当我需要在 3 维($this->config[one][two][tree])或一维($this->config[one])中获取数组值时,函数无法处理结果。我想获得 N 维数组。

我也尝试过这个解决方案: function nameToArray($name) { $tmp = explode('.',$name); $return = '';

    foreach($tmp as $v)
    {
        $return .= "['{$v}']";

    }

    return 'config'.$return;
}
function set($name,$value)
{
    $this->{$this->nameToArray} = $value;
}

$foo->set('site.favicon','something.ico');

但这不会在 $foo->config 中编辑数组,而是在 $this 中创建一个新值,字面意思是 config['site']['favicon']。

我不知道我该怎么做,我尝试了很多方法,但我无法得到预期的结果。感谢帮助。

4

3 回答 3

1

使用引用来引用数组中最新的已知点,并在每一步更新它:

$ref = &$this->config;
$keys = explode('.', $name);
foreach ($keys as $idx => $key) {
    if (!is_array($ref)) {
        // reference does not refer to an array
        // this is a problem as we still have at least one key to go
    }
    if (!array_key_exists($key, $ref)) {
        // key does not exist
        // reaction depends on the operation
    }
    // update reference
    $ref = &$ref[$key];
}

if分支取决于操作。下面是 getter 和 setter 的示例:

public function get($name) {
    $ref = &$this->config;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key) {
        if (!is_array($ref)) {
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx-1)).'" is not an array');
        }
        if (!array_key_exists($key, $ref)) {
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx)).'" does not exist');
        }
        $ref = &$ref[$key];
    }
    return $ref;
}
public function set($name, $value) {
    $ref = &$this->config;
    $keys = explode('.', $name);
    foreach ($keys as $idx => $key) {
        if (!is_array($ref)) {
            throw new Exception('key "'.implode('.', array_slice($keys, 0, $idx)).'" is not an array but '.gettype($ref));
        }
        if (!array_key_exists($key, $ref)) {
            $ref[$key] = array();
        }
        $ref = &$ref[$key];
    }
    $ref = $value;
}
于 2012-08-05T08:58:30.693 回答
1

我有另一个基于 eval 的解决方案我只是用括号连接字符串

<?php
Class foo {
    private $config=array();

    function __construct(){    
        $this->config['site']['title'] = 'testing';
        $this->config['site']['favicon'] = 'XXXXX.ico';
        $this->config['site']['keywords'] = array('page','cute','other');        
        $this->config['otherthing'] = 'value';
        /**etc**/
    }

    public function set($name,$value)
    {
        $tmp = explode('.',$name);
        $array_levels = "['";
        foreach($tmp as $key) {
            $array_levels .= $key."']['";
        }
        $array_levels = substr($array_levels, 0, -2);
        eval('$this->config'.$array_levels.' = $value;');
    }
    public function get($name)
    {
        $tmp = explode('.',$name);
        $array_levels = "['";
        foreach($tmp as $key) {
            $array_levels .= $key."']['";
        }
        $array_levels = substr($array_levels, 0, -2);
        eval('$value = $this->config'.$array_levels.';');
        return $value;
    }
}
$thing = new foo;
$thing->set('site.keywords',array('other','keywords'));
echo $thing->get('site.title');
于 2012-08-05T09:27:52.653 回答
0

正如您在此处看到的,该函数explode接受字符串并返回一个数组。所以这是你需要做的:

public function set($name,$value)
    {
        $tmp = explode('.',$name); //$tmp will now contain an array, so if $name = 'site.keywords'
                                   //$tmp[0] will contain 'site' and $tmp[1] = 'keywords'
        //if you send 'otherthing' as $name, then $tmp[1] will have no value
        if(isset($tmp[1])){
                $this->config[$tmp[0]][$tmp[1]] = $value; // this is like saying  $this-config['site']['keywords'] = $value
        }
        else{
            $this->config[$tmp[0]] = $value; 
        }
    }

为了得到:

public function get($name)
    {
        $tmp = explode('.',$name);
        if(isset($tmp[1])){
            return $this->config[$tmp[0]][$tmp[1]];
        }
        else{
           return $this->config[$tmp[0]];
        }

    }
于 2012-08-05T08:44:45.080 回答