We can perform operations on arrays using
Arrays Operators like Union, Equality,
Identity, Inequality and Non-identity.
List of Array Operators
Name Syntax Result
================================================================================
Union $array1 + $array2 Union of $array1 and $array2.
Equality $array1 == $array2 TRUE if $array1 and $array2 have the same key/value pairs.
Identity $array1 === $array2 TRUE if $array1 and $array2 have the same key/value pairs
in the same order and of the same types.
Inequality $array1 != $array2 TRUE if $a is not equal to $b.
Inequality $array1 <> $array2 TRUE if $a is not equal to $b.
Non-identity $array1 !== $array2 TRUE if $a is not identical to $b.
Union Array Operator (+)
The Union operator (+) appends right-hand oparand (array) to the left-hand oparand (array).
If there are keys that exist in both arrays, then the elements from the left-hand array will be used,
and the matching elements from the right-hand array will be ignored.
Example :
<?php
$array1 = array("a" => "Java", "b" => "Objective-C", "c" => ".Net");
$array2 = array( "a" => "Smalltalk","d" => "Pearl", "e" => "PHP");
$union1 = $array1 + $array2; // Union of $array1 and $array2
echo "Union of \$array1 and \$array2: <br>";
var_dump($union1);
$union2 = $array2 + $array1; // Union of $array2 and $array1
echo "<br>Union of \$array2 and \$array1: <br>";
var_dump($union2);
?>
Output
Union of $array1 and $array2:
array(5) {
["a"]=> string(4) "Java"
["b"]=> string(11) "Objective-C"
["c"]=> string(4) ".Net"
["d"]=> string(5) "Pearl"
["e"]=> string(3) "PHP"
}
Union of $array2 and $array1:
array(5) {
["a"]=> string(9) "Smalltalk"
["d"]=> string(5) "Pearl"
["e"]=> string(3) "PHP"
["b"]=> string(11) "Objective-C"
["c"]=> string(4) ".Net"
}
In the above example, key "a" in array1 has value "Java" where as array2 has the key "a" having value
as "Smalltalk". Thus,result of array1 + array2 will have key "a" from the left-hand array and the right hand key "a" element
will be ignored.
Equality Array Operator (==)
Equality Array Operator is used to check if both arrays have the same elements or not.
If array1 and array2 have the same elements (key value pairs) then boolean true is returned, else boolean false is
returned.
Example :
<?php
$array1 = array("Java","Smalltalk","Objective-C","Perl");
$array2 = array("Java","Smalltalk","Objective-C","Perl");
$array3 = array("Java","Smalltalk","Objective-C","Perl",".Net");
echo var_dump($array1 == $array2) ."<br>";
echo var_dump($array2 == $array3) ."<br>";
?>
Output
bool(true)
bool(false)
Inequality Array Operator (!=)
Inequality Array Operator is used to check if both arrays have the same elements or not.
If array1 and array2 have do not have same elements (key value pairs) then boolean false is returned, else boolean true is
returned.
Example :
<?php
$array1 = array("Java","Smalltalk","Objective-C","Perl");
$array2 = array("Java","Smalltalk","Objective-C","Perl");
$array3 = array("Java","Smalltalk","Objective-C","Perl",".Net");
echo var_dump($array1 != $array2) ."<br>";
echo var_dump($array2 != $array3) ."<br>";
?>
Output
bool(false)
bool(true)
Identity Array Operator (===)
Two arrays are Identical Only when the Keys and Values both are equal.
Example :
<?php
$array1 = array("PHP", "Java");
$array2 = array("1" => "Java", "0" => "PHP");
// == returns true
echo var_dump($array1 == $array2) ."<br>";
// === returns false as keys are different
echo var_dump($array1 === $array2) ."<br>";
?>
Output
bool(true)
bool(false)
Non-identity Array Operator (!==)
Two arrays are Non-identity when the Keys and Values both are not equal.
Example :
<?php
$array1 = array("PHP", "Java");
$array2 = array("1" => "Java", "0" => "PHP");
// == returns false
echo var_dump($array1 != $array2) ."<br>";
// === returns true as keys are different
echo var_dump($array1 !== $array2) ."<br>";
?>
Output
bool(false)
bool(true)