Check If Two Arrays Are Equal in PHP



Problem Description

Sometimes we need to compare two arrays in PHP to check if they are equal. Talking about equality, it means that the arrays have the same elements, in the same order, or even that their data types match. In this article, we will discuss how we can check if two arrays are equal or not in PHP.

The following are different approaches to check If two arrays are equal in PHP -

  • Brute Force Approach
  • Using the == Operator
  • Using array_diff() Method
  • Using the === Operator

Brute Force Approach

This approach comes to mind when we want to check if two arrays have same elements or not. In this approach, we first sort both arrays using sort() or a similar function. Then we use the == operator to compare them. We sort the array to ensure that the order of elements doesn't affect the comparison.

Example

<?php

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

// Sort both arrays
sort($array1);
sort($array2);

// Compare sorted arrays
if ($array1 == $array2) {
echo "The arrays are equal";
} else {
echo "The arrays are not equal";
}
?>

Output

The arrays are equal

Time Complexity: O(n log n)

Space Complexity: O(1)

Using the == Operator

This is the direct approach to checking if two arrays have the same elements or not. We use the == operator to check if two arrays have the same size and contain the same elements in the same order. This method is simple to use and this operator is not strict about data types. For example, 3 (integer) and '3' (string) are considered equal.

Example

<?php

// two arrays are equal or not
$arr1 = [1, 2, 3, 4];
$arr2 = [1, 2, 3, 4];

// Check if arrays are equal
if ($arr1 == $arr2) {
echo "The arrays are equal";
} else {
echo "The arrays are not equal";
}
?>

Output

The arrays are equal

Time Complexity: O(n)

Space Complexity: O(1)

Using array_diff() Method

This is an inbuilt method in PHP. This method is used to find the difference between two arrays. We use this function and check if it returns an empty array then it means both arrays are equal. This method is used to compare unordered arrays with unique elements.

Example

<?php

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

if (empty(array_diff($array1, $array2)) && empty(array_diff($array2, $array1))) {
echo "Arrays are equal";
} else {
echo "Arrays are not equal";
}

?>

Output

Arrays are equal

Time Complexity: O(n)

Space Complexity: O(n)

Using the === Operator

This operator makes strict checks on array elements. This operator checks:

If both arrays have the same size.
If both arrays contain the same elements in the same order.
If the types of elements are also the same.

If any of the above conditions fails, then this function returns false. This method makes a more precise comparison by taking types into consideration.

Example

<?php

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

if ($array1 === $array2) {
echo "The arrays are equal!";
} else {
echo "The arrays are not equal!";
}
?>

Output

The arrays are not equal!

Time Complexity: O(n)

Space Complexity: O(1)

Updated on: 2024-12-27T15:11:08+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements