Compare PHP Arrays by Keys, Values, and Missing Entries
Choose the right PHP array comparison: check whole-array equality, find shared values, or report missing values and key-value pairs.
If you need a yes-or-no answer about two whole PHP arrays, use ==. If you need to see which values they share or lack, use array_intersect() or array_diff(). When a value must also have the same key, use array_diff_assoc(). An intersection is not an equality verdict, and a difference reports entries from its first argument, not both sides at once.
Check whole-array equality with ==
PHP describes == as equality after type juggling. Its array comparison rules consider member count, keys, and values. That makes it useful for the whole-array question, but it is not a way to find the entry responsible for a failed check.
This example separates three cases that can otherwise look alike:
<?php
$record = ['name' => 'Ada', 'role' => 'editor'];
$reordered = ['role' => 'editor', 'name' => 'Ada'];
$numbers = [10, 20];
$swapped = [20, 10];
$integerId = ['id' => 1];
$stringId = ['id' => '1'];
echo "Same named pairs: ";
var_dump($record == $reordered);
echo "Swapped numbered values: ";
var_dump($numbers == $swapped);
echo "Integer and numeric string: ";
var_dump($integerId == $stringId);
Expected output:
Same named pairs: bool(true)
Swapped numbered values: bool(false)
Integer and numeric string: bool(true)
Reordering the named pairs preserves the value assigned to each key. Swapping the numbered values changes the value assigned to key 0. The last comparison succeeds because PHP compares a number and a numeric string numerically. Choose == with that type-juggling behavior in mind, particularly when the values did not originate in the same form.
Find shared values, not necessarily shared keys
For a report of common values, pass the arrays to array_intersect(). It returns values from the first array that exist in the other array, retaining their first-argument keys in this example. The PHP manual describes the match in terms of values, so a matching key in the second array is not required.
Use the same inputs for the intersection and both kinds of difference. The labels make the direction of each result explicit:
<?php
$expected = ['lead' => 'red', 'backup' => 'blue', 'spare' => 'amber'];
$received = ['primary' => 'red', 'backup' => 'blue', 'spare' => 'green'];
echo "Shared values from expected:\n";
print_r(array_intersect($expected, $received));
echo "Value differences from expected:\n";
print_r(array_diff($expected, $received));
echo "Value differences from received:\n";
print_r(array_diff($received, $expected));
echo "Key-value differences from expected:\n";
print_r(array_diff_assoc($expected, $received));
echo "Key-value differences from received:\n";
print_r(array_diff_assoc($received, $expected));
Expected output:
Shared values from expected:
Array
(
[lead] => red
[backup] => blue
)
Value differences from expected:
Array
(
[spare] => amber
)
Value differences from received:
Array
(
[spare] => green
)
Key-value differences from expected:
Array
(
[lead] => red
[spare] => amber
)
Key-value differences from received:
Array
(
[primary] => red
[spare] => green
)
The intersection includes red despite its different keys. It shows lead because $expected was the first argument. That shared value does not tell you whether the other entries match.
Report what is missing in each direction
array_diff($expected, $received) asks which values in $expected have no match in $received. Reverse the arguments to ask the reverse question. The array_diff() manual says its result preserves keys from the first array. Here, spare => amber comes from $expected, while spare => green comes from $received.
When the key-value pairing matters, use array_diff_assoc() in both directions. lead => red appears in the first associative difference because $received has red under primary, not lead. backup => blue appears in neither associative difference because that pair matches. The array_diff_assoc() manual documents the additional key check.
These functions match values when their string representations are identical. That is a different rule from =='s type-juggling comparison. Separate examples make the distinction visible:
<?php
$one = [1];
$stringOne = ['1'];
$padded = ['01'];
echo "Diff, 1 versus '1':\n";
print_r(array_diff($one, $stringOne));
echo "Diff, '01' versus 1:\n";
print_r(array_diff($padded, $one));
echo "Associative diff, 1 versus '1':\n";
print_r(array_diff_assoc($one, $stringOne));
echo "Associative diff, '01' versus 1:\n";
print_r(array_diff_assoc($padded, $one));
echo "Whole arrays, 1 versus '1': ";
var_dump($one == $stringOne);
echo "Whole arrays, '01' versus 1: ";
var_dump($padded == $one);
Expected output:
Diff, 1 versus '1':
Array
(
)
Diff, '01' versus 1:
Array
(
[0] => 01
)
Associative diff, 1 versus '1':
Array
(
)
Associative diff, '01' versus 1:
Array
(
[0] => 01
)
Whole arrays, 1 versus '1': bool(true)
Whole arrays, '01' versus 1: bool(true)
Each array here has key 0, so the associative differences turn on the value comparison. The string representations of 1 and '1' match; those of 1 and '01' do not. For ==, the whole arrays have corresponding keys, and PHP's numeric-string comparison makes both pairs of values equal.
Know what an empty difference cannot prove
Even two empty array_diff() results do not establish whole-array equality. A value match does not account for how many times that value appears:
<?php
$repeated = ['first' => 'red', 'second' => 'red'];
$single = ['only' => 'red'];
echo "Value differences from repeated:\n";
print_r(array_diff($repeated, $single));
echo "Value differences from single:\n";
print_r(array_diff($single, $repeated));
echo "Whole arrays equal: ";
var_dump($repeated == $single);
Expected output:
Value differences from repeated:
Array
(
)
Value differences from single:
Array
(
)
Whole arrays equal: bool(false)
Both red entries find a value match, but the arrays have different member counts. An intersection would be nonempty too. Return to == when the question is whether the whole arrays match.
For nested data, select the inner arrays whose fields you want to compare. The documented difference functions check only one dimension; an outer difference is not a field-level report.
<?php
$old = ['item' => ['id' => 1, 'color' => 'red']];
$new = ['item' => ['id' => 1, 'color' => 'blue']];
echo "Changed fields from old:\n";
print_r(array_diff_assoc($old['item'], $new['item']));
echo "Changed fields from new:\n";
print_r(array_diff_assoc($new['item'], $old['item']));
Expected output:
Changed fields from old:
Array
(
[color] => red
)
Changed fields from new:
Array
(
[color] => blue
)
Each result now names the changed inner entry and shows which selected array supplied its value.
