PHP 8 reference · Array Functions

PHP array_diff() Function

Return the values of the first array that are not in any of the others. Values are compared as strings.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_diff(array $array, array ...$arrays): array

Parameters

ParameterDescription
array The input array.
arrays Arrays whose values are excluded.

Return value

The values only in the first array (keys kept).

Examples

What's missingTry it yourself
<?php
print_r(array_diff(["a", "b", "c", "d"], ["b", "d"]));

Output

Array
(
    [0] => a
    [2] => c
)
Compare two listsTry it yourself
<?php
$before = ["tea", "cake", "milk"];
$after = ["tea", "milk", "jam"];
echo "removed: ", implode(",", array_diff($before, $after)), "\n";
echo "added: ", implode(",", array_diff($after, $before)), "\n";

Output

removed: cake
added: jam

Version history

VersionChange
PHP 4.0Added

In other languages

Was this page helpful?