PHP
📊 Array Operators in PHP
Array Operators are used to compare, combine, and manipulate PHP arrays. They allow developers to merge array elements, check equality, and evaluate strict identity between different data structures.
-
Key Concept: Array operations in PHP handle key-value pairs uniquely. For instance, union (
+) preserves keys from the left array and appends missing keys from the right array.
🛠️ PHP Array Operators Overview
PHP provides six core operators specifically designed for working with array data structures:
1️⃣ Union (+): Combines two arrays without overwriting existing keys.
2️⃣ Equality (==): Returns true if arrays have the same key/value pairs.
3️⃣ Identity (===): Returns true if arrays have the same key/value pairs in the same order and types.
4️⃣ Inequality (!= / <>): Returns true if arrays are not equal.
5️⃣ Non-identity (!==): Returns true if arrays are not identical.
🔍 Array Operator Code Examples
1️⃣ Array Union Operator (+)
Merges elements of the right array into the left array. If a key already exists in the left array, the right array's value for that key is ignored.
$x = ["a" => "red", "b" => "green"];
$y = ["c" => "blue", "b" => "yellow"];
$z = $x + $y; // Union of $x and $y
print_r($z);
// Output: Array ( [a] => red [b] => green [c] => blue )
// Note: 'b' => 'yellow' is ignored because 'b' already exists in $x.
2️⃣ Array Equality (==) vs. Identity (===) Operators
The equality operator checks for matching key/value pairs regardless of key order or type differences. The identity operator enforces identical order and data types.
$x = ["a" => "apple", "b" => "banana"];
$y = ["b" => "banana", "a" => "apple"];
var_dump($x == $y); // Returns: bool(true) (Same key/value pairs)
var_dump($x === $y); // Returns: bool(false) (Different order)
3️⃣ Array Inequality (!= / <>) and Non-Identity (!==) Operators
Evaluates whether two arrays differ in elements, key order, or internal data types.
$x = ["a" => "red", "b" => "green"];
$y = ["a" => "red", "b" => "blue"];
var_dump($x != $y); // Returns: bool(true) (Values for key 'b' differ)
var_dump($x !== $y); // Returns: bool(true) (Arrays are not identical)
📊 PHP Array Operators Reference Table
| Operator | Name | Example | Result | Description |
|---|---|---|---|---|
+ |
Union | $x + $y |
Array | Union of $x and $y (left key precedence). |
== |
Equality | $x == $y |
Boolean | true if $x and $y have the same key/value pairs. |
=== |
Identity | $x === $y |
Boolean | true if $x and $y have same key/value pairs in same order & types. |
!= |
Inequality | $x != $y |
Boolean | true if $x is not equal to $y. |
<> |
Inequality | $x <> $y |
Boolean | true if $x is not equal to $y. |
!== |
Non-identity | $x !== $y |
Boolean | true if $x is not identical to $y. |
-
📝 Short Summary
-
Use
+to combine arrays while keeping original keys intact from the left operand. -
Use
==when element order does not matter, and===when exact order and type precision are required. - Array comparison operators streamline data structure validation in PHP backend development.
