How to Find Common Elements in Two Arrays in PHP

Updated: January 10, 2024 By: Guest Contributor Post a comment

Overview

Arrays are fundamental components in PHP and comparing them is a common operation. Finding common elements in two arrays is a basic task that you might encounter in various PHP programming scenarios, such as comparing user inputs or filtering data based on certain conditions. PHP, being a server-side scripting language, offers several built-in functions to perform this task efficiently.

This guide dives into finding common elements between two arrays in PHP with several code examples, enhancing your array manipulation skills significantly.

Introduction

Using array_intersect

One of the most straightforward ways to find the common elements between two arrays is by using the array_intersect() function. This function compares the values of two arrays and returns the matches.

$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "berry", "cherry", "date"];

$common = array_intersect($array1, $array2);
print_r($common);

Output:

Array
(
    [1] => banana
    [2] => cherry
)

Using array_intersect_assoc

When you need to consider both the values and the keys as part of your comparison, array_intersect_assoc() comes into play. This function helps to find common elements with the same key and value pairs.

$array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$array2 = ["b" => "banana", "c" => "berry", "d" => "cherry"];

$common = array_intersect_assoc($array1, $array2);
print_r($common);

Output:

Array
(
    ["b"] => "banana"
)

Ignoring Case Sensitivity

Array comparisons in PHP are case-sensitive by default. However, you can use array_uintersect with a custom callback function to compare the values in a case-insensitive manner.

$array1 = ["Apple", "banana", "CHERRY"];
$array2 = ["banana", "berry", "cherry", "DATE"];

$common = array_uintersect($array1, $array2, "strcasecmp");
print_r($common);

Output:

Array
(
    [0] => Apple
    [1] => banana
    [2] => CHERRY
)

Combining Arrays before Comparison

If you need to find unique values from both arrays and then determine the common elements, you can combine them using array_merge followed by array_unique and array_intersect.

$array1 = ["apple", "banana", "cherry"];
$array2 = ["banana", "berry", "cherry", "apple"];

$combined = array_unique(array_merge($array1, $array2));
$common = array_intersect($array1, $array2, $combined);
print_r($common);

Output:

Array
(
    [1] => banana
    [2] => cherry
    [0] => apple
)

Advanced Array Comparisons

For more complex comparisons, you could use custom functions with usort and uksort for sorting by values or keys respectively, before using array_intersect to find common elements.

Here’s an example of advanced array comparisons using usort, uksort, and array_intersect in PHP:

// Sample arrays
$array1 = ['apple' => 1, 'banana' => 2, 'orange' => 3];
$array2 = ['banana' => 2, 'kiwi' => 4, 'grape' => 5];

// Custom comparison function for sorting by values
$valueComparison = function ($a, $b) {
    return $a <=> $b;
};

// Custom comparison function for sorting by keys
$keyComparison = function ($a, $b) {
    return strcmp($a, $b);
};

// Sort arrays by values and keys
uasort($array1, $valueComparison);
uksort($array2, $keyComparison);

// Find common elements using array_intersect
$commonElements = array_intersect($array1, $array2);

// Display the result
print_r($commonElements);

In this example, we use uasort to sort the first array by values and uksort to sort the second array by keys. Then, we use array_intersect to find common elements between the two arrays. The custom comparison functions ($valueComparison and $keyComparison) allow for more complex sorting criteria.

Conclusion

In concluding, comparing arrays and finding their common elements in PHP is an essential skill that is much easier thanks to PHP’s built-in functions. Knowing when and how to use these functions can significantly speed up your development process. The journey from basic to advanced techniques enhances not just code efficiency, but also deepens understanding of data operations in PHP.