drkjzk3359 2015-06-20 15:30
浏览 38
已采纳

我认为编辑数组值就像set(“a.b.c.d.e”,“我的新值”)但不起作用

i want edit a value of key like $class->set("this.is.a.children.key", "and this is my value").

My function:

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;
}

My Array:

$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"=>":)"
);

And my sentence:

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

1条回答 默认 最新

  • dongxie7683 2015-06-20 22:41
    关注

    As you are modifying the $source array within the rv method, you'll have to make it a reference. For the same reason, you should also remove the $this->config = ... assignment.

    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();
    

    Output:

    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
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?