PHP array_diff: How it Works with Examples

php array diff

The array_diff function compares arrays and returns values that exist in the first array but not in the others in PHP. This makes it useful when you need to filter unique items from a group of arrays.

Understand the array_diff Function in PHP

The function checks each value in the first array. It then removes values that also exist in the second array or in the next arrays. The order stays the same as the input array.

Here is the syntax:

array_diff(array $array1, array $array2, array $array3...): array
  • The first parameter is the main array.
  • The rest are arrays to compare against.
  • It returns an array that holds only unique values from the first array.

Here is a quick example:

$first = [1, 2, 3, 4];
$second = [3, 4, 5];
$result = array_diff($first, $second);
print_r($result);

The output:

Array
(
[0] => 1
[1] => 2
)

This example gives [1, 2] because 3 and 4 exist in the second array. The function does not compare keys, it only checks values.

When you pass nested arrays, array_diff does not look deep inside. It only checks top values and ignores sub arrays. To compare nested data, you must use another method.

The array_diff with Associative Arrays

Associative arrays also work with array_diff. The function checks only the values and keeps the original keys in the result. Keys do not affect the comparison process.

Here is an example:

$arr1 = [
    "a" => "red",
    "b" => "green",
    "c" => "blue",
    "d" => "yellow"
];

$arr2 = [
    "e" => "red",
    "f" => "blue",
    "g" => "black"
];

$result = array_diff($arr1, $arr2);

print_r($result);

The output:

Array
(
[b] => green
[d] => yellow
)

The Differences Between array_diff and array_intersect

The array_diff function returns values that are unique to the first array. The array_intersect function does the opposite and returns values that exist in all arrays.

Here is a table that shows you the key differences:

FunctionPurposeOutput Example
array_diffValues unique to first array[1, 2]
array_intersectValues common in all arrays[3, 4]

You use array_diff when you need items excluded. You use array_intersect when you need items included across all arrays.

Examples of the array_diff Function in PHP

Compare Two Arrays of Numbers:

$a = [10, 20, 30, 40];
$b = [30, 40, 50];
$result = array_diff($a, $b);
print_r($result);

The output:

Array
(
[0] => 10
[1] => 20
)

This code gives [10, 20]. The result has values that only exist in the first array. You can use this method to remove unwanted numbers from a data set.

Compare Associative Arrays with Values:

$first = ["x" => 100, "y" => 200, "z" => 300];
$second = ["a" => 200, "b" => 400];
$result = array_diff($first, $second);
print_r($result);

The output:

Array
(
[x] => 100
[z] => 300
)

The output is [100, 300]. Keys do not matter in this case. The function only compares values and keeps the original key structure.

Use array_diff with Multiple Arrays:

$main = [1, 2, 3, 4, 5, 6];
$x = [2, 4];
$y = [6, 7];
$result = array_diff($main, $x, $y);
print_r($result);

The output:

Array
(
[0] => 1
[2] => 3
[4] => 5
)

The result is [1, 3, 5]. The function removed values that exist in both the second and third arrays. This is useful when you must filter a list against many sets of data.

Wrapping Up

In this article you learned the syntax of array_diff and its use with both indexed arrays and associative arrays. You also saw how it behaves with nested arrays, how it compares to array_intersect, and how it works with multiple arrays.

Here is a quick recap:

  • array_diff returns values unique to the first array.
  • It ignores keys and only checks values.
  • It can compare multiple arrays at once.
  • Use array_intersect to find common values instead.

FAQs

What does PHP array_diff function do?

The array_diff function compares arrays and returns values from the first array that are not in the others.
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "yellow");

$result = array_diff($array1, $array2);
print_r($result);
Output will be:

Array
(
    [b] => green
    [c] => blue
)

How to use PHP array_diff with multiple arrays?

You can pass more than two arrays into array_diff. It will check against all arrays and return only unique values from the first.
$array1 = array("apple", "banana", "mango", "grape");
$array2 = array("banana", "grape");
$array3 = array("apple");

$result = array_diff($array1, $array2, $array3);
print_r($result);
Output will be:

Array
(
    [2] => mango
)

What is the difference between array_diff and array_intersect in PHP?

  • array_diff returns values in the first array not in others.
  • array_intersect returns values present in all arrays.
$array1 = array("red", "green", "blue");
$array2 = array("green", "yellow", "blue");

$diff = array_diff($array1, $array2);
$intersect = array_intersect($array1, $array2);

print_r($diff);
print_r($intersect);
Output will be:

Array
(
    [0] => red
)

Array
(
    [1] => green
    [2] => blue
)

Similar Reads

PHP array_keys: How to Extract Keys in Arrays with Examples

This guide shows how PHP array_keys finds all keys in an array and how you can filter them by a…

PHP explode Function: How it Works with Examples

When you deal with text in PHP, sometimes you need to break it into parts. This is common when users…

PHP array_multisort Function with Examples

PHP array_multisort sorts many arrays or a multi level array in one go. It can sort data in numeric order…

PHP Predefined Constants Explained Simply

PHP has a number of predefined constants that give you some information about the language and its environment. You can…

PHP Integers Guide: Types, Limits, and Conversions

You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…

PHP mb_strtolower: How to Handle Multibyte Strings Properly

The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…

PHP JSON Handling with json_encode & json_decode

JSON is used in most of your web applications for dealing with data, and so, in a way, PHP JSON…

PHP AND (&&) Operator: Usage & Examples

As you are entering into PHP, it would be essential to understand how logical operators work. The PHP AND operator…

filter_has_var Function: Check if Input Exists in PHP

User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

Previous Article

HTML del Tag: How to Mark Deleted Text in Pages

Next Article

PHP array_is_list: Check if an Array is a List with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.