Account
Categories

Union Operator(+)


Definition:

The symbol of this operator is +.

Its function is to add two or more arrays and store all values in a third array.

Before storing, it checks the keys.

It matches the keys of the first array with the keys of the other arrays.

If the same key is found, it takes the key and value from the first array.

Then it adds all the keys and values from the other arrays that are not repeated, and places them in the order of the first array.

Syntax:

$array1 + $array2

Example:

$array1 = array("a" => "Apple", "b" => "Banana");
$array2 = array("b" => "Berry", "c" => "Cherry");
$result = $array1 + $array2;
print_r($result);

Output:

Array
(
    [a] => Apple
    [b] => Banana
    [c] => Cherry
)