PHP - Array Operators

PHP provides array operators that allow developers to compare and manipulate arrays efficiently. These operators help in combining arrays, checking equality, and comparing their values and keys.

Types of PHP Array Operators

OperatorNameDescription
+UnionCombines two arrays (ignores duplicate keys)
==EqualityReturns true if both arrays have the same key-value pairs
===IdentityReturns true if both arrays have the same key-value pairs and the same order
!= or <>InequalityReturns true if arrays are not equal
!==Non-identityReturns true if arrays are not identical

Let's explore these operators with practical examples.


1. PHP Union Operator (+)

The union operator (+) merges two arrays. If duplicate keys exist, values from the first array are preserved.

Example

<?php $array1 = ["a" => "Apple", "b" => "Banana"]; $array2 = ["b" => "Berry", "c" => "Cherry"]; $result = $array1 + $array2; print_r($result); ?>

Output:

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

Explanation:

  • $array1 and $array2 have the key "b", but the value from $array1 is retained.

  • The "c" => "Cherry" pair from $array2 is added.


2. PHP Equality Operator (==)

The equality operator (==) checks if two arrays have the same key-value pairs (order doesn't matter).

Example

<?php $array1 = ["x" => 10, "y" => 20]; $array2 = ["y" => 20, "x" => 10]; var_dump($array1 == $array2); // Output: bool(true) ?>

Explanation:

  • The keys and values are the same, so the arrays are considered equal.


3. PHP Identity Operator (===)

The identity operator (===) checks if two arrays are exactly the same (same key-value pairs in the same order).

Example

<?php $array1 = ["x" => 10, "y" => 20]; $array2 = ["y" => 20, "x" => 10]; var_dump($array1 === $array2); // Output: bool(false) ?>

Explanation:

  • The arrays have the same key-value pairs, but their order is different, so they are not identical.


4. PHP Inequality Operator (!= or <>)

The inequality operator (!= or <>) checks if two arrays are not equal.

Example

<?php $array1 = ["a" => "Car", "b" => "Bike"]; $array2 = ["a" => "Car", "b" => "Bus"]; var_dump($array1 != $array2); // Output: bool(true) ?>

Explanation:

  • The values for key "b" are different, so the arrays are not equal.


5. PHP Non-identity Operator (!==)

The non-identity operator (!==) checks if two arrays are not identical.

Example

<?php $array1 = ["p" => 100, "q" => 200]; $array2 = ["q" => 200, "p" => 100]; var_dump($array1 !== $array2); // Output: bool(true) ?>

Explanation:

  • The key-value pairs match, but the order is different, so they are not identical.


Why Use PHP Array Operators?

  • Efficiently merge arrays – The union operator (+) helps combine arrays while preserving unique keys.

  • Perform array comparisons – The equality (==) and identity (===) operators are useful in checking similarities between arrays.

  • Detect array differences – The inequality (!=) and non-identity (!==) operators help spot discrepancies in arrays.


Key Takeaways

The union operator (+) merges arrays, keeping values from the first array if keys overlap.
The equality (==) operator checks if two arrays have the same key-value pairs, regardless of order.
The identity (===) operator ensures both arrays are identical in both value and order.
The inequality (!= or <>) and non-identity (!==) operators detect differences between arrays.


Conclusion

PHP provides several array operators to help developers perform operations like merging, comparing, and differentiating arrays efficiently. By understanding these operators, you can manage data structures more effectively in PHP applications.