Change language

PHP Check if two arrays contain the same elements

PHP has two types of arrays: indexed arrayand associative array . In the case of an indexed array, the elements of the array are indexed numerically, starting at 0, where, as with an associative array, the elements of the array have named keys associated with them. 
Now, to check if two arrays are equal, you can iterate over the arrays and check if for each index the value associated with the index in both arrays is the same or not. PHP has a built-in array operator (===) to check for the same, but the order of the array elements is not important here. When the order of the array elements is not important, two methods can be used to check if arrays are equal:
  • Use the sort() function to sort an array element, and then use the equal operator.
  • Use the array operator (==) in case of an associative array.
Checking for equality in an indexed array:this can be used on a numeric array where integer indexing of the string. Here, use the sort() function to sort the elements of the array, and then use the equality operator to check the index of the two arrays using the array operator. Here, since the order of the array elements is not important, the sort will perform all the array elements sequentially, so if two arrays are equal, the values ​​corresponding to the same index of both arrays will be the same.Program :PHP program for checking equality of two arrays using sorting.  
// Program for checking array equality using
// sort() and the equality operator 
// Sample Arrays $arr1 = array (4, 5, ’hello’ , 2.45, 3.56); $arr2 = array (5, 2.45, ’hello’ , 3.56, 4);  
// Sort array elements sort ( $arr1 ); sort ( $arr2 );  
// Test for equality if ( $arr1 == $arr2 ) echo "Both arrays are same" ; else echo " Both arrays are not same " ;  
// Sample Arrays $arr3 = array ( 5, ’car’ , ’ hello’ , 2.45, 3.56); $arr4 = array (4, 2.45, ’hello’ , 3.56, ’engineer’ );  
// Sort array elements sort ( $arr3 ); sort ( $arr4 );  
// Test for equality if ( $arr3 == $arr4 ) echo "Both arrays are same" ; else echo " Both arrays are not same " ;  
?>
Exit:
Both arrays are same Both arrays are not same 
Test for equality in an associative array.In case of an associative array, all elements have an index associated with them, so no sorting is required, the equality operator can be directly applied to test for equality. In fact, the equality operator compares the values ​​corresponding to the index in both 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 an indexed array, the sort is performed to arrange the elements sequentially, whereas in the case of an associative array, the elements are already indexed, so the sort is no longer required.Program:PHP code to check equality of two associative arrays  
// Program for checking associative array equality 
// Sample Arrays $arr1 = array ( ’ first’ = > ’engineer’ , ’second’ = > ’for’ , ’last’ = > ’ide’ ); $arr2 = array ( ’first’ = > ’ engineer’ , ’last’ = > ’ ide’ , ’second’ = > ’for’ );  
// Test for equality if ( $arr1 == $arr2 ) echo "Both arrays are same" ; else echo " Both arrays are not same " ;  
// Sample Arrays $arr3 = array ( ’first’ = > ’ engineer’ , ’second’ = > ’for’ , ’last’ = > ’ ide ’ ); $arr4 = array ( ’first’ = > ’ geek’ , ’second’ = > ’ for’ , ’last’ = > ’engineer’ );  
// Test for equality if ( $arr3 == $arr4 ) echo "Both arrays are same" ; else echo " Both arrays are not same " ;  
?>
Exit:
Both arrays are same Both arrays are not same 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method