An in depth analysis of the difference between php using the plus sign and array_merge to merge arrays
- 2020-06-03 06:02:02
- OfStack
Let's start with two arrays
Let's use array_merge and the plus sign instead of the two arrays
As you can see here, array_merge is used to merge the values of the array 1 to the end of the first array. If the array contains the numeric key name, the value will not be overridden, but appended. However, if the key name is the same, the array value appears first and is ignored
So let's change the array that we gave you earlier
As you can see here, array_merge is used to merge the values of the array 1 to the end of the first array. If the non-numeric key names are the same, the value of the subsequent array overrides the value of the previous array. However, if the key name is the same, the array value appears first and is ignored
<?php
$r = array(1,2,3,4,5,6);
$e = array(7,8,9,10);
?>
Let's use array_merge and the plus sign instead of the two arrays
<?php
print_r($r+e); // The output <span style="font-family: Simsun;font-size:16px; ">Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) </span>
print "<br />";
print_r(array_merge($r,$e)); // The output <span style="font-family: Simsun;font-size:16px; ">Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )</span>
?>
As you can see here, array_merge is used to merge the values of the array 1 to the end of the first array. If the array contains the numeric key name, the value will not be overridden, but appended. However, if the key name is the same, the array value appears first and is ignored
So let's change the array that we gave you earlier
<?php
$r = array('r'=>1,2,3,4,5,6);
$e = array(<span style="background-color: rgb(245, 250, 255); ">'r'=></span>7,8,9,10);
?>
<?php
print_r($r+e); // The output Array ( [r] => 1 [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 )
print "<br />";
print_r(array_merge($r,$e)); // The output Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
?>
As you can see here, array_merge is used to merge the values of the array 1 to the end of the first array. If the non-numeric key names are the same, the value of the subsequent array overrides the value of the previous array. However, if the key name is the same, the array value appears first and is ignored