Check if Two Arrays Contain the Same Elements in PHP

In PHP, you may often need to determine whether two arrays contain the same elements, regardless of their order.

Checking for array equality can be useful in various scenarios, such as comparing user input with a reference array or ensuring the consistency of data.

In this article, we'll explore different methods to check if two arrays contain the same elements in PHP.

Method 1: Using array_diff()

One straightforward approach to check if two arrays contain the same elements is to use the array_diff() function.

This function calculates the difference between two arrays and returns an array containing the values from the first array that are not present in the second array.

Here's an example:

$array1 = [1, 2, 3, 4, 5];
$array2 = [5, 4, 3, 2, 1];

$difference = array_diff($array1, $array2);

if (empty($difference)) {
    echo "The arrays contain the same elements.";
} else {
    echo "The arrays do not contain the same elements.";
}

Output:

The arrays contain the same elements.

In this example, we have two arrays, $array1 and $array2, containing the same elements but in different orders.

  1. We use array_diff($array1, $array2) to calculate the difference between them.
  2. If the resulting array is empty, it means the arrays contain the same elements.

Method 2: Using array_diff_assoc()

If you want to compare arrays with both the same elements and the same keys, you can use the array_diff_assoc() function.

This function compares the keys and values of two arrays, returning the values from the first array that are not present in the second array.

Here's an example:

$array1 = ["a" => 1, "b" => 2, "c" => 3];
$array2 = ["c" => 3, "b" => 2, "a" => 1];

$difference = array_diff_assoc($array1, $array2);

if (empty($difference)) {
    echo "The arrays contain the same elements and keys.";
} else {
    echo "The arrays do not contain the same elements and keys.";
}

Output:

The arrays contain the same elements and keys.

In this example, both arrays have the same elements with the same keys, albeit in a different order.

array_diff_assoc($array1, $array2) is used to compare them. If the resulting array is empty, it means the arrays contain the same elements and keys.


Method 3: Using Custom Comparison

If you need a more customized comparison and don't want to rely on built-in functions, you can write a custom function to check for array equality.

This approach allows you to implement specific comparison logic tailored to your needs.

Here's an example of a custom function:

function arraysAreEqual($array1, $array2) {
    if (count($array1) !== count($array2)) {
        return false;
    }

    foreach ($array1 as $value) {
        if (!in_array($value, $array2)) {
            return false;
        }
    }

    return true;
}

$array1 = [1, 2, 3, 4, 5];
$array2 = [5, 4, 3, 2, 1];

if (arraysAreEqual($array1, $array2)) {
    echo "The arrays contain the same elements.";
} else {
    echo "The arrays do not contain the same elements.";
}

Output:

The arrays contain the same elements.
  1. In this custom function, we first check if the arrays have the same length. If not, they cannot contain the same elements.
  2. Then, we iterate through the elements of the first array and use in_array() to check if each element exists in the second array.
  3. If any element is not found, the arrays are not considered equal.

Conclusion

Checking if two arrays contain the same elements in PHP is a common task, and you can choose the method that best suits your requirements.

Whether you prefer using array_diff() or array_diff_assoc() for simplicity or implementing a custom comparison function for more control, these methods enable you to determine array equality and ensure the consistency of your data in PHP applications.