PHP中array_replace和array_merge之间的区别 [英] Differences between array_replace and array_merge in PHP
问题描述
我正在尝试找出 array_replace()之间的区别和 array_merge().在这篇文章之后,这个问题实际上浮现在我的脑海: PHP array_merge空值总是优先级较低,实际上可以使用这两个功能中的任何一个来解决问题.因此,我试图找出在哪些情况下我们应该使用array_replace而不是array_merge,反之亦然.
I am trying to figure out the differences between array_replace() and array_merge(). The question actually came to my mind after this post : PHP array_merge empty values always less prioritar, where the problem actually can be solved with any of these two functions. So, I was trying to find out in which cases we should use array_replace instead of array_merge and vice versa.
在阅读了两个函数的php文档之后,我发现了这两个区别:
After reading the php documentation for both functions, I find these two differences :
- 如果数组包含数字键,则后一个值将不会覆盖
array_merge()
中的原始值,这将在array_replace()
中完成. - 在
array_merge()
中,具有数字键的输入数组中的值将使用从结果数组中的零开始的递增键重新编号,而array_replace()
则不会发生这种情况.
- If the arrays contain numeric keys, the later value will not overwrite the original value in
array_merge()
, which will be done inarray_replace()
. - In
array_merge()
, values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array, which shouldn't happen witharray_replace()
.
由于差异仅与数字键有关,因此我们可以肯定地说,在处理关联数组时,函数array_replace()
和array_merge()
是完全等效的吗?还是我还缺少其他区别?
Since the differences are only related to numeric keys, can we safely say that, functions array_replace()
and array_merge()
are exactly equivalent when we are dealing with associative arrays? Or is there any other difference which I am missing?
推荐答案
对于具有字符串键的数组,是的,就像您提到的那样,它们是等效的.如果您有数字键,array_merge()
将根据需要附加它们,甚至在必要时对其重新排序,而array_replace()
将覆盖原始值.
For arrays with string keys, yes these are equivalent, as you mentioned. If you have numeric keys, array_merge()
will append them as required, and even re-order them if necessary, whereas array_replace()
will overwrite the original values.
例如,
$a = array('a' => 'hello', 'b' => 'world');
$b = array('a' => 'person', 'b' => 'thing', 'c'=>'other', '15'=>'x');
print_r(array_merge($a, $b));
/*Array
(
[a] => person
[b] => thing
[c] => other
[0] => x
)*/
print_r(array_replace($a, $b));
/*Array
(
[a] => person
[b] => thing
[c] => other
[15] => x
)*/
如您所见,array_merge
重新索引了数组的数字键,并且它们两个都只更新了字符串键.
As you can see, array_merge
has re-indexed the numeric keys of the array, and both of them simply update string keys.
但是,当您拥有数字键时,array_merge()
根本不关心键,而是按看到的顺序添加所有内容,不删除任何内容,而array_replace()
顾名思义,将用类似的(数字)替换键)索引:
However, when you have numeric keys, array_merge()
will simply not care about keys, and add everything in the order it sees, deleting nothing, whereas array_replace()
will, as the name suggests, replace keys with similar (numeric) indices:
<?php
$a = array('0'=>'a', '1'=>'c');
$b = array('0'=>'b');
print_r(array_merge($a, $b));
/*Array
(
[0] => a
[1] => c
[2] => b
)*/
print_r(array_replace($a, $b));
/*Array
(
[0] => b
[1] => c
)*/
这篇关于PHP中array_replace和array_merge之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!