PHP更改多维数组的值

4

我有一个问题。

我写了一个函数来更新我的config.json文件。问题是,我的config.json是一个多维数组。为了获取一个键的值,我使用了这个函数:

public function read($key)
{
    $read   = explode('.', $key);
    $config = $this->config;
    foreach ($read as $key) {
        if (array_key_exists($key, $config)) {
            $config = $config[$key];
        }
    }

    return $config;
}

我也创建了一个更新键的函数。但问题是,如果我执行update('database.host', 'new value');,它不仅会更新该键,还会覆盖整个数组。
这是我的更新函数:
public function update($key, $value)
{
    $read   = explode('.', $key);
    $config = $this->config;
        foreach ($read as $key) {
        if (array_key_exists($key, $config)) {
            if ($key === end($read)) {
                $config[$key] = $value;
                                     }
                $config = $config[$key];
                                              }
                                }
        print_r( $config );
}

我的config.json文件看起来像这样:

{
    "database": {
        "host": "want to update with new value",
        "user": "root",
        "pass": "1234",
        "name": "dev"
    },
    some more content...
}

我有一个工作函数,但并不是很好。我知道索引的最大值只能为三,所以我计算了爆炸的 $key 并更新了值:

public function update($key, $value)
{
    $read  = explode('.', $key);
    $count = count($read);
        if ($count === 1) {
        $this->config[$read[0]] = $value;
    } elseif ($count === 2) {
        $this->config[$read[0]][$read[1]] = $value;
    } elseif ($count === 3) {
        $this->config[$read[0]][$read[1]][$read[3]] = $value;
    }
        print_r($this->config);
}

只是需要知道:变量$this->config是我的config.json解析为PHP数组,所以没有问题 :)
2个回答

1
在我仔细阅读了您的问题后,现在我明白您想要什么了。尽管您的读取函数不是很清晰,但它可以正常工作。
您的更新可以通过使用引用赋值符号&来改进,以遍历索引并将新值分配给数组的正确元素。
下面的代码完成的操作是将完整的配置对象分配给临时变量newconfig,使用引用调用,这意味着每当我们更改newconfig变量时,我们也会更改this->config变量。使用这个“技巧”多次,最终我们可以将新值分配给newconfig变量,并且由于引用调用分配,this->config对象的正确元素应该被更新。
public function update($key, $value)
{
    $read  = explode('.', $key);
    $count = count($read);

    $newconfig = &$this->config; //assign a temp config variable to work with
    foreach($read as $key){
        //update the newconfig variable by reference to a part of the original object till we have the part of the config object we want to change.
        $newconfig = &$newconfig[$key];
    }
    $newconfig = $value;


    print_r($this->config);
}

如果我没有一个多维数组,这个方法就可以运行。因为我传递了字符串"database.host",所以该函数应该更新$this->config['database']['host']的值,但你的函数正在更新$this->config['database']到新值,这意味着它覆盖了其他子数组,如主机、用户和密码。我希望你明白我的意思,我的英语不是很好:s - user5629061
你可以使用 is_array($value) 来检查值是否为数组,并以递归方式调用该函数来解决你面临的维度问题。 - Bas van Stein
你能发一个例子吗?我不确定你的意思。 - user5629061
很少有人现在我更好地理解你的问题,看看我的更新答案。那应该能改善你的更新代码。 - Bas van Stein

-1
你可以尝试这样做:
public function update($path, $value)
{
    array_replace_recursive(
        $this->config,
        $this->pathToArray("$path.$value")
    );

    var_dump($this->config);
}

protected function pathToArray($path)
{
    $pos = strpos($path, '.');
    if ($pos === false) {
        return $path;
    }

    $key = substr($path, 0, $pos);
    $path = substr($path, $pos + 1);

    return array(
        $key => $this->pathToArray($path),
    );
}

请注意,您可以改进它以接受所有数据类型的值,而不仅仅是标量类型。

我希望我能够以过程式编程的方式拥有这个,而不是面向对象编程。 - phpN00b

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接