PHP array_diff() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_diff() returns values from the first array that are missing from the other arrays.

You use array_diff() to find what is new, missing or left over, for example the tags that were removed or the ids that are not yet processed. It only looks at the values, never at the keys.

Syntax

Call array_diff() with the values you need. Start with this line.

PHP
$missing = array_diff($all, $done);
CallWhat it does
array_diff([1, 2, 3, 4], [2, 4])Returns [0 => 1, 2 => 3], the values that are not in the second array.
array_diff($a, $b, $c)Returns values of $a that are in neither $b nor $c.
array_diff($a, [])Returns all values of $a, because there is nothing to remove.

The keys of the first array are kept, so the result can have gaps like 0, 2, 5. Wrap it in array_values() if you want a clean list that starts at 0. Values are compared as strings.

Library declarations
PHP
array_diff(array $array, array ...$arrays): array

Parameters

ParameterDescription
arrayRequired. The array to compare from. Its keys are kept in the result.
arraysOptional. One or more arrays to compare against. A value that is found in any of them is removed from the result.

Return Value

Return ValueDescription
arrayThe values of the first array that are not present in any other array.

PHP Examples

Example 1 — Finding Values That Are Not Shared

Remove from the first list every value that also exists in the second list.

PHP
<?php
$all = [1, 2, 3, 4, 5];
$remove = [2, 4];

print_r(array_diff($all, $remove));

How It Works

  1. array_diff() looks at each value in $all.
  2. The values 2 and 4 are also in $remove, so they are left out.
  3. The values 1, 3 and 5 are kept with their original keys 0, 2 and 4.
  4. The gaps in the keys show that nothing was re-indexed.

Example 2 — String Comparison and Kept Keys

Values are compared as strings, so 1 and '1' are equal. The keys of the first array are preserved.

PHP
<?php
$a = [1, '1', 2];
$b = ['1'];

print_r(array_diff($a, $b));

How It Works

  1. The integer 1 at key 0 turns into the string '1' for the comparison.
  2. The string '1' at key 1 is also equal to the value in $b.
  3. Both of them are removed from the result.
  4. Only the value 2 remains, and it keeps its original key, 2.

Example 3 — Several Arrays and Re-indexing

You can compare against more than one array. Use array_values() to get a clean list again.

PHP
<?php
$letters = ['a', 'b', 'c', 'd'];

$result = array_diff($letters, ['a'], ['d']);

echo implode(',', array_keys($result)), PHP_EOL;
echo implode(',', array_keys(array_values($result))), PHP_EOL;
echo implode(',', $result), PHP_EOL;

How It Works

  1. 'a' appears in the second array and 'd' appears in the third, so both are removed.
  2. The values 'b' and 'c' remain with their original keys 1 and 2.
  3. array_values() builds a new list whose keys are 0 and 1.
  4. implode() joins the remaining values into 'b,c'.

Frequently Asked Questions

It returns the values from the first array that are not found in any of the other arrays. It is used to find what is different or missing.
No. It only compares values. Use array_diff_key() to compare keys, or array_diff_assoc() to compare both keys and values.
array_diff() keeps the keys from the first array. Call array_values() on the result when you want the keys to be 0, 1, 2 and so on.
It converts each value to a string and checks that the strings are identical. So 1 and '1' are equal, but 'a' and 'A' are not.
Use array_intersect(). It returns the values that are present in the first array and in all the other arrays.

Next: array_diff_assoc()

array_diff_assoc() compares both the keys and the values of the first array with other arr.

array_diff_assoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful