我遇到了问题。这是我的代码:
$a = ['elm1' => 1, 'elm2' => []];
$b = ['elm1' => 2, 'elm2' => [3]];
$c = array_replace_recursive($b, $a);
在 $c 中,我希望看到 ['elm1' => 1, 'elm2' => []],但是我得到 ['elm1' => 1, 'elm2 ' => [3]]。它不会将 'elm2' => [3] 替换为 'elm2' => []。
这是某种功能还是 array_replace_recursive 中的错误?
谢谢。
最佳答案
而不是 array_replace_recursive
你需要简单的array_replace
作为
$a = ['elm1' => 1, 'elm2' => []];
$b = ['elm1' => 2, 'elm2' => [3]];
$c = array_replace($b, $a);
print_r($c);//['elm1' => 1, 'elm2' => []]
关于php - array_replace_recursive 不会用一些空数组替换数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312042/