Php change value for multidimensional array

I have a problem.

I am creating a function to update my config.json file. The problem is that my config.json is a multidimensional array. To get the key value, I use this function:

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

I also made a function to update the key. But the problem is that if I create update('database.host', 'new value');, it does not only update this key, but overrides the entire array.

This is my update function.

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

my config.json looks like this:

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

I have a working function, but that is not very good. I know that the maximum of indices can only be three, so I count the exploded key $ and update the value:

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

Just know: the variable $this->configis my config.json processed in the php array, so there was nothing wrong with that :)

+4
2

, , , , , , .

, & .

, 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);
}
+2

- :

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

, , ,

-1

Source: https://habr.com/ru/post/1619880/


All Articles