To find the union of two maps in PHP, we can use the array_merge()
function. This function takes two or more arrays as arguments and returns a new array that contains all the elements from the input arrays.
To merge two maps, simply pass them as arguments to array_merge()
. However, if both maps have the same key, only the value from the last map will be included in the merged array.
Here's an example:
main.php147 chars7 lines
The output will be:
main.php75 chars9 lines
In this example, the keys 'a', 'b', and 'c' are in $map1
, and the keys 'c', 'd', and 'e' are in $map2
. After merging the two maps, the resulting array includes all six key/value pairs. The value for 'c' in $map2
replaces the value in $map1
.
gistlibby LogSnag