Open In App

PHP Check if two arrays contain same elements

Last Updated : 03 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to compare the elements of arrays in PHP, & will know how to apply to get the result after comparison through the examples. In PHP, there are two types of arrays, Indexed array and Associative array. In an Indexed array, the array elements are index numerically starting from 0 while in an Associative array, the array elements have named keys associated with them. 

Now, to check whether two arrays are equal or not, an iteration can be done over the arrays and check whether for each index the value associated with the index in both the arrays is the same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements is not important. When the order of the array elements is not important, two methods can be applied to check the array equality which is listed below:

  • Use the sort() function to sort an array element and then use the equality operator.
  • Use array operator ( == ) in case of Associative array.

We will understand both the ways to compare the elements in arrays.

Equality checking in an indexed array: 

This can be applied in the numeric array where string integer indexing is done. Here, use the sort() function to sort array elements and then use the equality operator for checking the index of those two arrays using the array operator. Here is the order of the array elements are not important, so sorting will make all the array element in a sequential manner, thus if two arrays are equal the values corresponding to the same index of both the arrays will be the same.

Example: PHP example to check equality of two arrays using the sorting technique and equal operator.

PHP




<?php
  $arr1 = array(4, 5, 'hello', 2.45, 3.56);
  $arr2 = array(5, 2.45, 'hello', 3.56, 4);
 
  // Sort the array elements
  sort($arr1);
  sort($arr2);
 
  // Check for equality
  if ($arr1 == $arr2)
      echo "Both arrays are same\n";
  else
      echo "Both arrays are not same\n";
 
  $arr3 = array(5, 'car', 'hello', 2.45, 3.56);
  $arr4 = array(4, 2.45, 'hello', 3.56, 'geeks');
 
  // Sort the array elements
  sort($arr3);
  sort($arr4);
 
  // Check for equality
  if ($arr3 == $arr4)
      echo "Both arrays are same";
  else
      echo "Both arrays are not same";
?>


Output: 

Both arrays are same
Both arrays are not same

 

Equality checking in Associative array: In the case of an associative array, all the elements have an index associated with them so, there is no need for sorting, the equality operator can be directly applied to check for equality. Basically, the equality operator compares the values corresponding to an index in both the arrays if all the index values are the same then they are equal otherwise they are not.

Syntax:

bool $arr1 == $arr2

In the case of the indexed array, the sorting is done to arrange the elements sequentially whereas in the case of the associative array the elements are already indexed so sorting is not necessary anymore.

Example: PHP example to check the equality of two associative arrays.

PHP




<?php
  $arr1 = array( 'first' => 'geeks',
                 'second' => 'for',
                 'last' => 'ide'
          );
  $arr2 = array( 'first' => 'geeks',
                 'last' =>'ide',
                 'second' =>'for'
          );
 
  // Check for equality
  if ($arr1 == $arr2)
      echo "Both arrays are same\n";
  else
      echo "Both arrays are not same\n";
 
  $arr3 = array( 'first' => 'geeks',
                 'second' => 'for',
                 'last' => 'ide'
          );
  $arr4 = array( 'first' => 'geek',
                 'second' =>'for',
                 'last' =>'geeks'
          );
 
  // Check for equality
  if ($arr3 == $arr4)
      echo "Both arrays are same";
  else
      echo "Both arrays are not same";
?>


Output: 

Both arrays are same
Both arrays are not same

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads