Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

PHP Array Operators

PHP Array Operators

Demystifying PHP Array Operators: A Guide with Examples

Arrays are fundamental building blocks in PHP, and manipulating them efficiently requires a grasp of array operators. This article dives into the different types of array operators in PHP, providing clear explanations and practical examples for each.

1. Union Operator (+)

The + operator in PHP doubles as both an arithmetic and array operator. When applied to arrays, it performs a union operation, appending the elements of the right-hand array to the left-hand array.

Example:

$fruits1 = ["apple", "banana", "orange"];
$fruits2 = ["mango", "grapes", "apple"];

$combinedFruits = $fruits1 + $fruits2;

var_dump($combinedFruits);

This code results in the $combinedFruits array containing “apple”, “banana”, “orange”, “mango”, “grapes”. Notice that the duplicate “apple” from the second array is ignored.

2. Intersection Operator (array_intersect)

Unlike the union operator, array_intersect finds common elements between two arrays. It returns a new array containing only elements that exist in both the input arrays.

Example:

$favoriteFruits = ["apple", "banana", "strawberry"];
$seasonalFruits = ["banana", "mango", "guava"];

$commonFruits = array_intersect($favoriteFruits, $seasonalFruits);

var_dump($commonFruits);

This code outputs “banana” as the only common element present in both arrays.

3. Difference Operator (array_diff)

The array_diff function takes two arrays and returns an array containing elements present in the first array but not in the second.

Example:

$availableFruits = ["apple", "banana", "orange", "mango"];
$purchasedFruits = ["apple", "banana"];

$remainingFruits = array_diff($availableFruits, $purchasedFruits);

var_dump($remainingFruits);

This code results in $remainingFruits containing “orange” and “mango”.

4. Array Comparison Operators

Several comparison operators work with arrays in PHP. These include ==, ===, !=, and !==, which compare the values and data types of arrays for equality or difference.

Example:

$arr1 = [1, 2, 3];
$arr2 = [1, 2, 4];

var_dump($arr1 == $arr2); // false - values differ
var_dump($arr1 === $arr2); // false - data types differ
var_dump($arr1 != $arr2); // true - values differ
var_dump($arr1 !== $arr2); // true - data types differ

5. Spread Operator (...)

Introduced in PHP 7.4, the spread operator allows expanding an array within another. It comes in handy for combining or merging arrays.

Example:

$breakfast = ["eggs", "toast"];
$beverages = ["coffee", "tea"];

$fullBreakfast = [...$breakfast, ...$beverages];

var_dump($fullBreakfast);

This code outputs “eggs”, “toast”, “coffee”, “tea” in the $fullBreakfast array.

Conclusion

Understanding and utilizing array operators effectively is crucial for manipulating and managing data efficiently in PHP projects. By leveraging these operators, you can streamline your code and achieve desired results with ease.

Scroll to Top