php - array_replace_recursive 不会用一些空数组替换数组

标签 php arrays

我遇到了问题。这是我的代码:

$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' => []]

Fiddle

关于php - array_replace_recursive 不会用一些空数组替换数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312042/

相关文章:

javascript - Javascript 中数组索引不会越界

php - 使用 PHP 的 TNT Express Connect 定价模块

php - MYSQL 选择文件

javascript - 如何使用单个模态显示多个图像

c - 如何节省数组中未使用的空间?

c - 预测号码

php - 在哪里以及如何设置 Vary : User-Agent HTTP Header

php - 向首次访问者显示欢迎信息

javascript - 函数仅接收字符串数组的第一个元素

arrays - 将数组中的所有项复制到引用类型的新数组中的函数