我认为编辑数组值就像set(" a.b.c.d.e","我的新值")但是不行

时间:2015-06-20 15:30:29

标签: php arrays recursion

我想要修改像$class->set("this.is.a.children.key", "and this is my value")这样的密钥值。

我的功能:

public function set ($key, $value) {
    if (array_key_exists($key, $this->config)) {
        $this->config[$key] = $value;
        return true;
    } else {
        if(strpos($key, ".")){
            $keys = explode(".", $key);
            if (count($keys) > 0) {
                if (array_key_exists($keys[0], $this->config)) {
                    function rv($source, $array_keys, $value){
                        if (count($array_keys) == 1) {
                            $source[$array_keys[0]] = $value;
                        } else {
                            return rv(
                                $source[$array_keys[0]],
                                array_slice($array_keys, 1),
                                $value
                            );
                        }
                    }
                    $this->config = rv($this->config, $keys, $value);
                }
            }
        }
    }
    return false;
}

我的阵列:

$this->config = array(
    "a"=>array(
        "b"=>1,
        "c"=>array(
            "d"=>array(
                "e"=>2,
                "f"=>"x",
                "g"=>null,
            )
        ),
        "h"=>null,
        "i"=>102.2,
        "j"=>array(
            3=>3
        )
    ),
    "k"=>":)"
);

我的一句话:

$this->set("k", "This is K");
$this->set("a.c.d.g", "This is a -> c -> d -> g");

1 个答案:

答案 0 :(得分:1)

当您在$source方法中修改rv数组时,您必须将其作为参考。出于同样的原因,您还应该删除$this->config = ...作业。

class Foo
{
    public $config = array(
        "a" => array(
            "b" => 1,
            "c" => array(
                "d" => array(
                    "e" => 2,
                    "f" => "x",
                    "g" => null,
                )
            ),
            "h" => null,
            "i" => 102.2,
            "j" => array(
                3 => 3
            )
        ),
        "k" => ":)"
    );

    public function set($key, $value)
    {
        if (array_key_exists($key, $this->config)) {
            $this->config[$key] = $value;
            return true;
        } else {
            if (strpos($key, ".")) {
                $keys = explode(".", $key);
                if (count($keys) > 0) {
                    if (array_key_exists($keys[0], $this->config)) {
                        // Added & to $source here
                        function rv(&$source, $array_keys, $value)
                        {
                            if (count($array_keys) == 1) {
                                $source[$array_keys[0]] = $value;
                            } else {
                                // No need for return statement
                                rv(
                                    $source[$array_keys[0]],
                                    array_slice($array_keys, 1),
                                    $value
                                );
                            }
                        }

                        // No assignment
                        rv($this->config, $keys, $value);
                    }
                }
            }
        }
        return false;
    }

    public function test()
    {
        $this->set("k", "This is K");
        $this->set("a.c.d.g", "This is a -> c -> d -> g");
        print_r($this->config);
    }
}

(new Foo())->test();

输出:

Array
(
    [a] => Array
        (
            [b] => 1
            [c] => Array
                (
                    [d] => Array
                        (
                            [e] => 2
                            [f] => x
                            [g] => This is a -> c -> d -> g
                        )

                )

            [h] => 
            [i] => 102.2
            [j] => Array
                (
                    [3] => 3
                )

        )

    [k] => This is K
)