猿问

array_replace_recursive 覆盖键而不是附加键

我试图用另一个数组覆盖或替换一个数组。为此,我尝试了 array_replace_recursive 函数,但我得到了我似乎无法清除的不需要的行为。

我有两个数组:

原始数组:

[test] => Array
            [me] => Array
                    [0] => test
                    [1] => me
                    [2] => now
            [me2] => Array
                    [0] => test
                    [1] => me
                    [2] => now

我要替换的数组

[test] => Array
            [me2] => Array
                    [name] => firstname
                    [last] => lastname

使用 array_replace_recursive($first_array, $second_array) 后,我得到以下输出:

 [test] => Array
            [me] => Array
                    [0] => test
                    [1] => me
                    [2] => now
            [me2] => Array
                    [0] => test <-- this needs to be overwritten not appended
                    [1] => me <-- this needs to be overwritten not appended
                    [2] => now <-- this needs to be overwritten not appended
                    [name] => firstname
                    [last] => lastname

我真的需要覆盖 ['test']['me2'] 值而不是附加。所以 0,1,2 键应该消失了。有人可以在这里指出我正确的方向吗?


哈士奇WWW
浏览 111回答 2
2回答

幕布斯7119047

您可能必须编写自己的函数,以您认为合适的方式进行替换。我认为应该:接受两个数组 ($a,$b) 作为参数如果 $a[KEY] 是一个数组且 $b[KEY] 未设置,则保留 $a[KEY]如果 $a[KEY] 是一个数组并且 $b[KEY] 是一个数组,调用这个方法 w/ ($a[KEY] & $b[KEY])如果 $a 的子节点不是数组且 $b 有子节点,则将 $a 替换为 $b或者类似的东西......我很难概念化你到底需要什么,并为它编写一个函数,我意识到有些极端情况可能会出现在更复杂的数组中。所以我写了这个函数和测试。我只使用您提供的示例数组对其进行了测试,但它提供了正确的输出。如果您向数组添加另一层,或者其中有一个数组,其中有一些是数组的孩子和一些不是数组的孩子,这可能会出现问题。<?phpfunction recurseReplace($a,$b){&nbsp; &nbsp; $ret = [];&nbsp; &nbsp; foreach ($a as $key=>$value){&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($b[$key])&&is_array($value)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ret[$key] = $value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($value)&&isset($b[$key])&&is_array($b[$key])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ret[$key] = recurseReplace($value,$b[$key]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (count($ret)==0){&nbsp; &nbsp; &nbsp; &nbsp; foreach ($b as $key=>$value){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ret[$key] = $value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $ret;}$a = [&nbsp; &nbsp; "test" => [&nbsp; &nbsp; &nbsp; &nbsp; "me"=>['test','me','now'],&nbsp; &nbsp; &nbsp; &nbsp; "me2"=>["test",'me','now']&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];$b = [&nbsp; &nbsp; "test" => [&nbsp; &nbsp; &nbsp; &nbsp; "me2"=>["name"=>'firstname',"last"=>"lastname"]&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];$desired = [&nbsp; &nbsp; "test" => [&nbsp; &nbsp; &nbsp; &nbsp; "me"=>['test','me','now'],&nbsp; &nbsp; &nbsp; &nbsp; "me2"=>["name"=>'firstname',"last"=>"lastname"]&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ];$final = recurseReplace($a,$b);echo "\n\n-----final output::---\n\n";print_r($final);echo "\n\n-----desired::---\n\n";print_r($desired);

江户川乱折腾

芦苇,谢谢你这对我有用..但我从你的代码中得到启发并开始改进它。对于其他需要这样的人:&nbsp; &nbsp; function conf_get($paths, $array) {&nbsp; &nbsp; &nbsp; &nbsp; $paths = !is_array($paths) ? [] : $paths;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($paths as $path)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $array = $array[$path];&nbsp; &nbsp; &nbsp; &nbsp; return $array;&nbsp; &nbsp; }&nbsp; &nbsp; function conf_set($paths, $value, $array) {&nbsp; &nbsp; &nbsp; &nbsp; $array = !is_array($array) ? [] : $paths; // Initialize array if $array not set&nbsp; &nbsp; &nbsp; &nbsp; $result = &$array;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($paths as $i => $path) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($i < count($paths) - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!isset($result[$path]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$path] = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result = &$result[$path];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result[$path] = $value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }&nbsp; &nbsp; $config = [];&nbsp; &nbsp; $config ['test']['me'] = ['test', 'me', 'now'];&nbsp; &nbsp; $config ['test']['me2'] = ['test', 'me', 'now'];echo "\n INITIAL CONFIG";&nbsp;print_r($config );echo "\n GET PATH test";&nbsp;print_r(conf_get('test', $config));echo "\n GET PATH test,me1" ;print_r(conf_get(['test', 'me2'], $config);echo "\n REPLACE PATH test,me2 with new array" ;print_r(conf_set(['test', 'me2'], ['name' => 'firstname', 'last' => 'lastname'], $config), "");echo "\n ADD PATH test,me6 with new array";print_r(conf_set(['test', 'me6'], ['name' => 'firstname', 'last' => 'lastname'], $config));结果:[INITIAL CONFIG]&nbsp; &nbsp; [test] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [me] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => me&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => now&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [me2] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => me&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => now[GET PATH test]&nbsp; &nbsp; [test] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [me] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => me&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => now&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [me2] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => me&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => now[GET PATH test,me1]&nbsp; &nbsp; [0] => test&nbsp; &nbsp; [1] => me&nbsp; &nbsp; [2] => now[REPLACE PATH test,me2 with new array]&nbsp; &nbsp; [me2] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => firstname&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [last] => lastname[ADD PATH test,me6 with new array]&nbsp; &nbsp; [me6] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [name] => firstname&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [last] => lastname
随时随地看视频慕课网APP
我要回答