# Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

## PHP: Array Operators

Description

This is a Comprehensive PHP array operators tutorial from w3resource.com

List of array operators

 Name Example Result Union \$x + \$y Union of \$x and \$y. The + operator appends elements of remaining keys from the right-sided array to the left-handed, but duplicated keys are not overwritten. Equality \$x == \$y TRUE if \$x and \$y have the same key/value pairs. Identity \$x === \$y TRUE if \$x and \$y have the same key/value pairs in the same order and of the same types. Inequality \$x != \$y TRUE if \$x is not equal to \$y. Inequality \$x <> \$y TRUE if \$x is not equal to \$y. Non-identity \$x !== \$y TRUE if \$x is not identical to \$y.

Example : array union (+) operator

In the following example, the union operator adds the last element from the \$b array (\$c = \$a + \$b) with \$a array as "c=>" key is not present in \$a array. In the second statement (\$c = \$b + \$a) no element is added from \$b as all the keys of \$a array are present in \$b array.

1. \$a = array("a" => "apple", "b" => "banana");

2. \$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

3. \$c = \$a + \$b; // Union of \$a and \$b

4. echo "Union of \$a and \$b :
";

5. var_dump(\$c);

6. \$c = \$b + \$a; // Union of \$b and \$a

7. echo "
Union of \$b and \$a :
";

8. var_dump(\$c);

9. ?>

Output :

Union of \$a and \$b :
array(3) { ["a"]=> string(5) "apple" ["b"]=> string(6) "banana" ["c"]=> string(6) "cherry" }
Union of \$b and \$a :
array(3) { ["a"]=> string(4) "pear" ["b"]=> string(10) "strawberry" ["c"]=> string(6) "cherry" }

View the example in the browser

Example : array equality (==) and identity(===) operators

In the following example equality operator returns true as the two arrays have same key/value pairs whereas identity operator returns false as the key/value of the comparing arrays are same but not in same order.

1. \$a = array("1" => "apple", "0" => "banana");

2. \$b = array( "banana", "apple");

3. var_dump(\$a == \$b);

4. var_dump(\$a === \$b);

5. ?>

Output :

bool(true) bool(false)