PHP中array_replace和array_merge之间的差异

时间:2022-05-10 13:25:40

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.

我试图找出array_replace()和array_merge()之间的差异。在这篇文章之后,实际上我想到了这个问题:PHP array_merge空值总是更少,其中问题实际上可以通过这两个函数中的任何一个来解决。所以,我试图找出在哪些情况下我们应该使用array_replace而不是array_merge,反之亦然。

After reading the php documentation for both functions, I find these two differences :

在阅读了两个函数的php文档后,我发现了这两个不同之处:

  1. If the arrays contain numeric keys, the later value will not overwrite the original value in array_merge(), which will be done in array_replace().
  2. 如果数组包含数字键,则后面的值不会覆盖array_merge()中的原始值,这将在array_replace()中完成。

  3. 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 with array_replace().
  4. 在array_merge()中,带有数字键的输入数组中的值将使用从结果数组中的零开始的递增键重新编号,而array_replace()不应该发生这种情况。

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_replace()和array_merge()是完全等价的吗?或者我还缺少其他任何差异?

2 个解决方案

#1


7  

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.

对于带有字符串键的数组,是的,这些是等效的,正如您所提到的。如果你有数字键,array_merge()将根据需要附加它们,甚至在必要时重新排序它们,而array_replace()将覆盖原始值。

For example,

$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
)*/

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已重新索引数组的数字键,并且它们都只是更新字符串键。

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:

但是,当你有数字键时,array_merge()根本不关心键,并按照它看到的顺序添加所有内容,什么都不删除,而array_replace()将顾名思义用相似的(数字)索引替换键:

<?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
)*/

#2


4  

Jarek gave a nice explanation in his article here:

Jarek在他的文章中给出了一个很好的解释:

https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/

He also adds in the use of the + operator with arrays for comparison.

他还在使用带有数组的+运算符进行比较。

PHP中array_replace和array_merge之间的差异

#1


7  

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.

对于带有字符串键的数组,是的,这些是等效的,正如您所提到的。如果你有数字键,array_merge()将根据需要附加它们,甚至在必要时重新排序它们,而array_replace()将覆盖原始值。

For example,

$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
)*/

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已重新索引数组的数字键,并且它们都只是更新字符串键。

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:

但是,当你有数字键时,array_merge()根本不关心键,并按照它看到的顺序添加所有内容,什么都不删除,而array_replace()将顾名思义用相似的(数字)索引替换键:

<?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
)*/

#2


4  

Jarek gave a nice explanation in his article here:

Jarek在他的文章中给出了一个很好的解释:

https://softonsofa.com/php-array_merge-vs-array_replace-vs-plus-aka-union/

He also adds in the use of the + operator with arrays for comparison.

他还在使用带有数组的+运算符进行比较。

PHP中array_replace和array_merge之间的差异