本文介绍了array_replace_recursive不会将数组替换为空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了问题.这是我的代码:
I've got the problem. Here is my code:
$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'=> [] .
In $c I expect to see ['elm1' => 1, 'elm2' => []], however I get ['elm1' => 1, 'elm2' => [3]]. It does not replace 'elm2' => [3] with 'elm2' => [].
这是某种功能还是在array_replace_recursive中存在错误?
Is this some kind of feature or this is a bug in array_replace_recursive?
谢谢.
推荐答案
使用 array_merge()
:-
<?php
$a = ['elm1' => 1, 'elm2' => []];
$b = ['elm1' => 2, 'elm2' => [3]];
$c = array_merge($b,$a); //it will check the indexes in both array and if indexes are same then first array value will remain on that index second will replace.
echo "<pre/>";print_r($c);
?>
这篇关于array_replace_recursive不会将数组替换为空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!