How to check if Two Arrays are Equal in PHP - Step by Step Examples
How to check if Two Arrays are Equal in PHP ?
Answer
To check if two arrays are equal in PHP, you can use the array_diff
function for arrays with the same elements and the ==
operator for arrays with different elements.
✐ Examples
1 Arrays with Same Elements
In this example,
- We create two arrays named
$arr1
and$arr2
with the same elements. - We then use the
array_diff
function to check if the arrays are equal and store the result in the variable$equal1
. - The
array_diff
function compares the values of two arrays and returns the difference. - Finally, we check if
$equal1
is empty to determine if the arrays are equal.
PHP Program
<?php
$arr1 = [1, 2, 3];
$arr2 = [1, 2, 3];
$equal1 = empty(array_diff($arr1, $arr2));
echo "Arrays are equal: " . ($equal1 ? 'true' : 'false');
?>
Output
Arrays are equal: true
2 Arrays with Different Elements
In this example,
- We create two arrays named
$arr3
and$arr4
with different elements. - We then use the
==
operator to check if the arrays are equal and store the result in the variable$equal2
. - The
==
operator checks if two arrays are equal in terms of key/value pairs, regardless of order. - Finally, we print the value of
$equal2
to the console.
PHP Program
<?php
$arr3 = [1, 2, 3];
$arr4 = [3, 2, 1];
$equal2 = $arr3 == $arr4;
echo "Arrays are equal: " . ($equal2 ? 'true' : 'false');
?>
Output
Arrays are equal: false
Summary
In this tutorial, we learned How to check if Two Arrays are Equal in PHP language with well detailed examples.
More PHP Arrays Tutorials
- How to Declare an Array in PHP ?
- How to Initialize an Array in PHP ?
- How to Access Array Elements in PHP ?
- How to Access Array Elements using Index in PHP ?
- How to get First Element in Array in PHP ?
- How to get Last Element in Array in PHP ?
- How to check if an Array is Empty in PHP ?
- How to check if an Array is Not Empty in PHP ?
- How to get Sub Array in PHP ?
- How to Get Array Length in PHP ?
- How to Iterate Over an Array in PHP ?
- How to Iterate Over an Array in Reverse Order in PHP ?
- How to get the Index of Specified Element in an Array in PHP ?
- How to check if Specified Element is present in the Array in PHP ?
- How to count the Number of Occurrences of Specified Element in the Array in PHP ?
- How to Sort an Array in PHP ?
- How to Sort an Array in Ascending Order in PHP ?
- How to Sort an Array in Descending Order in PHP ?
- How to create a Two Dimensional Array in PHP ?
- How to Iterate over a Two Dimensional Array in PHP ?
- How to create a Three Dimensional Array in PHP ?
- How to Copy an Array in PHP ?
- How to Split an Array in PHP ?
- How to Join Arrays in PHP ?
- How to check if Two Arrays are Equal in PHP ?
- How to check if Two Arrays have Same Elements (Regardless of Order) in PHP ?
- How to Convert an Array of Integers to an Array of Strings in PHP ?
- How to Convert an Array of Strings to an Array of Integers in PHP ?
- How to Reverse an Array in PHP ?
- How to Shuffle an Array in PHP ?
- How to Rotate Elements in an Array in PHP ?
- How to Filter Elements of an Array based on a Condition in PHP ?
- How to Declare an Integer Array in PHP ?
- How to Declare a Float Array in PHP ?
- How to Declare a String Array in PHP ?
- How to Remove Duplicates in an Array in PHP ?
- How to Remove Specific Element from an Array in PHP ?
- How to Remove Element from Array based on a Condition in PHP ?
- How to Sort a String Array in Dictionary Order in PHP ?
- How to Concatenate Strings in Array in PHP ?