我不想使用array_merge(),因为它导致我误解了具有相同键的所有值都将被覆盖.我有两个阵列
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
Run Code Online (Sandbox Code Playgroud)
并希望将它们组合起来,就像这样
array(0=>'foo', 1=>'bar',2=>'bar', 3=>'foo');
Run Code Online (Sandbox Code Playgroud)
array_merge() 将第二个数组的值附加到第一个数组.它不会覆盖密钥.
你的例子,导致:
数组([0] => foo [1] => bar [2] => bar [3] => foo)
然而, If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
除非这只是你遇到的另一个问题的一个例子吗?
如果有人偶然发现这一点,这是当今的一种方法:
var_dump(array_merge_recursive($array1, $array2));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7679 次 |
| 最近记录: |