How to Find the Difference Between Two Arrays in PHP

PHP has built-in functions for almost every flavor of array difference you'd need - by value, by key, or with a custom comparison. Here are the four approaches worth knowing, in order of how often you'll actually reach for each.

Quick answer

array_diff($a, $b) returns values from $a that aren't in $b, comparing as strings and keeping $a's original keys. For case-insensitive comparison, use array_udiff($a, $b, 'strcasecmp'). To compare by key instead of value, use array_diff_key($a, $b). For symmetric difference, combine both directions: array_merge(array_diff($a, $b), array_diff($b, $a)).

Need the same thing in another language?

See the full reference for Python, JavaScript, SQL, Java, and more.

See every language →

array_diff(): the standard approach

$a = ['apple', 'banana', 'cherry', 'banana'];
$b = ['banana', 'date'];

$aOnly = array_diff($a, $b);
print_r($aOnly);
// [0 => 'apple', 2 => 'cherry'] - original keys kept, duplicate 'banana' at key 3 removed too

array_diff() compares values as strings internally and keeps the original keys from $a - note the gap where key 1 and 3 used to be. If you need a clean, re-indexed array, wrap the result in array_values().

array_diff_key() and array_diff_assoc(): comparing by key

$a = ['id1' => 'apple', 'id2' => 'banana', 'id3' => 'cherry'];
$b = ['id2' => 'banana', 'id4' => 'date'];

$aOnly = array_diff_key($a, $b);
print_r($aOnly);
// ['id1' => 'apple', 'id3' => 'cherry'] - compares keys, ignores values entirely

array_diff_key() ignores values completely and only checks whether each key exists in the other array - useful for associative arrays where you care which keys are present, not what they're set to. array_diff_assoc() is a middle ground: it requires both the key and the value to match for an entry to be considered "in both".

array_udiff(): custom comparison and case-insensitivity

$a = ['Apple', 'Banana', 'Cherry'];
$b = ['banana', 'date'];

$aOnly = array_udiff($a, $b, 'strcasecmp');
print_r($aOnly);
// ['Apple', 'Cherry']

array_udiff() takes a comparison callback as its last argument - any function with the signature of strcmp() (returning negative, zero, or positive) works, including your own closure for comparing objects or numeric ranges instead of plain strings.

array_flip() + isset(): the fast manual approach

$a = ['apple', 'banana', 'cherry', 'banana'];
$b = ['banana', 'date'];

$lookup = array_flip($b);
$aOnly = array_filter($a, fn($x) => !isset($lookup[$x]));
print_r(array_values($aOnly));
// ['apple', 'cherry'] - duplicate 'banana' still filtered, order preserved

Writing this by hand with in_array($x, $b) works but is O(n * m), since in_array() scans the whole of $b for every item. array_flip($b) turns $b's values into keys, so isset($lookup[$x]) becomes an O(1) lookup and the whole filter drops to roughly O(n + m) - though for most cases, array_diff() is simpler and already handles this internally.

Just need the answer, not the code? Paste both lists.

Open Compare 2 Lists →

Frequently Asked Questions

What is the standard way to find the difference between two arrays in PHP?
array_diff($a, $b) returns every value from $a that does not appear in $b, keeping the original keys of $a. It compares values as strings internally, which is usually fine but can surprise you with mixed types.
Why does array_diff compare values as strings?
array_diff casts values to strings for comparison (equivalent to (string) $val1 === (string) $val2), so 1, '1', and 1.0 are all treated as equal. This is usually what you want for plain lists, but it means objects without a __toString() method, or arrays used as values, will throw a warning or produce unreliable results - use array_udiff with a custom comparison callback for those cases.
What is the difference between array_diff and array_diff_key?
array_diff() compares array values and ignores keys entirely - it just checks whether each value from $a exists anywhere in $b's values. array_diff_key() compares keys instead, ignoring values completely - useful for comparing associative arrays where you care about which keys exist, not what they're set to.
How do I do a case-insensitive array difference in PHP?
Use array_udiff($a, $b, 'strcasecmp'), which runs strcasecmp as the comparison callback instead of PHP's default case-sensitive string comparison. For non-string values, write your own callback function with the same signature (return negative, zero, or positive like strcmp) and pass it as the third argument.
How do I get the symmetric difference between two PHP arrays?
Combine both one-way diffs: array_merge(array_diff($a, $b), array_diff($b, $a)). This gives every value that exists in exactly one of the two arrays.
Is array_diff slow for large arrays?
array_diff() is optimized internally and generally performs well, but a manual array_filter($a, fn($x) => !in_array($x, $b)) is O(n * m) because in_array() does a linear scan of $b for every item. If you write it manually, flip $b into a key-based lookup first with array_flip($b) and use isset() for O(1) checks instead.
How do I preserve array keys from the original array when finding the difference?
array_diff() already preserves the original keys of the first array in its result by default - it only re-indexes if you explicitly do so afterward with array_values(). If you need the difference to be re-indexed from 0, wrap the call: array_values(array_diff($a, $b)).

Share this tool

Featured on ToolDirs
© 2026 ListDiff

Free online list and text processing tools.