How to Check If Two Arrays Are Equal in PHP?

Learn how to check one-dimensional or multidimensional arrays for equality in PHP

Whether you're checking for equality on flat arrays or multidimensional ones (with either numeric, associative or mix keys), if you follow these rules of array equality, comparing arrays in PHP should be quite straightforward:

Comparing Arrays Using Strict Equality (===)

Two arrays (for example, $a and $b) are strict equal (i.e. $a === $b) when:

  1. they have the same key/value pairs;
  2. the key/values are in the same order;
  3. the values are of the same types.

Comparing Arrays Using Loose Equality (==)

Two arrays (for example, $a and $b) are loose equal (i.e. $a == $b) when they have the same key/value pairs regardless of their order and/or type.

The use of loose equality is generally discouraged, you should always try to use strict equality. This means that you should ensure the types and order of the array elements you're comparing are the same.

Examples of Array Comparison

The following examples show the result of array comparison using the strict and loose equalities:

Arrays Having the Same Key/Value Pairs in the Same Order With Values of the Same Types:

$a = ['a', 'b', 'c'];
$b = ['a', 'b', 'c'];

// the above is equivalent to:

$a = ['a', 'b', 'c'];
$b = [0 => 'a', 1 => 'b', 2 => 'c'];

var_dump($a == $b); // true
var_dump($a === $b); // true
$a = ['a', 'b', 'c', [ 'foo' => 'bar' ]];
$b = ['a', 'b', 'c', [ 'foo' => 'bar' ]];

// the above is equivalent to:

$a = ['a', 'b', 'c', [ 'foo' => 'bar' ]];
$b = [0 => 'a', 1 => 'b', 2 => 'c', 3 => [ 'foo' => 'bar' ]];

var_dump($a == $b); // true
var_dump($a === $b); // true

Arrays Having the Same Key/Value Pairs in the Same Order With Different Key Types:

$a = [0 => 'a', 1 => 'b', 2 => 'c'];
$b = [false => 'a', true => 'b', '2' => 'c'];

var_dump($a == $b); // true
var_dump($a === $b); // true

From the above example you can see that the types of keys are not strictly compared in either of the equality.

Arrays Not Having the Same Key/Value Pairs:

In the following examples, the two arrays have different keys. Therefore, the key/value pairs of the two arrays are not equal in either of the equality comparisons:

$a = ['a', 'b'];
$b = ['b', 'a'];

// the above is equivalent to:

$a = [0 => 'a', 1 => 'b'];
$b = [0 => 'b', 1 => 'a'];

var_dump($a == $b); // false
var_dump($a === $b); // false
$a = [1 => 'a', 2 => 'b', [ 'foo' => 'bar' ]];
$b = [[ 'foo' => 'bar' ], 2 => 'b', 1 => 'a'];

// the above is equivalent to:

$a = [
    1 => 'a',
    2 => 'b',
    3 => [ 'foo' => 'bar' ]
];

$b = [
    0 => [ 'foo' => 'bar' ],
    2 => 'b',
    1 => 'a'
];

var_dump($a == $b); // false
var_dump($a === $b); // false

Arrays Not Having the Same Type:

$a = [1, 2, 3];
$b = ['1', 2, 3];

var_dump($a == $b); // true
var_dump($a === $b); // false
$a = [false, true];
$b = [0, 2];

var_dump($a == $b); // true
var_dump($a === $b); // false
$a = ['0.0', '2'];
$b = [0, 2];

var_dump($a == $b); // true
var_dump($a === $b); // false

Instead of using loose equality, prior to comparing you could ensure all elements of array have the correct types (for example by using array_map(), array_walk(), etc.), and compare with strict equality.

Arrays Not Having the Same Order:

$a = [1 => 'a', 2 => 'b'];
$b = [2 => 'b', 1 => 'a'];

var_dump($a == $b); // true
var_dump($a === $b); // false
$a = [1 => 'a', 2 => 'b', 'baz' => [ 'foo' => 'bar' ]];
$b = ['baz' => [ 'foo' => 'bar' ], 2 => 'b', 1 => 'a'];

var_dump($a == $b); // true
var_dump($a === $b); // false

Instead of using loose equality, you could use one of the array sorting functions (such as sort(), etc.) to sort both arrays, and then compare them with strict equality.


Hope you found this post useful. It was published (and was last revised ). Please show your love and support by sharing this post.