An easy way to share an array with a plus sign in PHP

  • 2020-03-31 21:32:38
  • OfStack

Code:
 
<?php 
$a = array('a' => 'a', 'b' => 'b'); 
$b = array('c' => 'c', 'd' => 'd'); 
$c = $a + $b; 
print('<pre>'); 
print_r($c); 
print('</pre>'); 
?> 

Results:
 
Array 
( 
[a] => a 
[b] => b 
[c] => c 
[d] => d 
) 

Note: the difference between the plus sign and the array_merge() function is that when you merge an array with the plus sign, if there are keys of the same name between the arrays, you keep the corresponding values of the previous array, whereas the array_merge() function does the opposite.

Related articles: