Skip to content
Related Articles

Related Articles

Improve Article
PHP | Check if two arrays contain same elements
  • Last Updated : 14 Mar, 2019

In PHP there are two types of arrays, Indexed array and Associative array. In case of Indexed array the array elements are index numerically starting from 0 where as in case of Associative array the array elements has 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 index in both the arrays is same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements are not important. When the order of the array elements are not important, two methods can be applied to check the array equality which are listed below:

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

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

Program: PHP program to check equality of two arrays using sorting.




<?php
      
// Program to check equality of array by using
// sort() and equal operator
      
// Sample arrays
$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";
      
// Sample arrays
$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 case of associative array, all the elements have an index associated with them so, there is no need of sorting, the equality operator can be directly applied to check for equality. Basically the equality operator compare the values corresponding to an index in both the arrays if all the index values are same then they are equal otherwise they are not.



Syntax: bool $arr1 == $arr2

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

Program: PHP code to check equality of two associative array




<?php
      
// Program to check equality of associative array
      
// Sample arrays
$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";
      
// Sample arrays
$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



My Personal Notes arrow_drop_up
Recommended Articles
Page :