array_diff_assoc()

array_diff_assoc() — Computes the difference of arrays with additional index check.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Compares array against arrays and returns the difference. Unlike array_diff() the array keys are also used in the comparison.

Signature

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

Parameters

ParameterTypeWhat it takes
$arrayarrayThe array to compare from
...$arraysarrayArrays to compare against

Return value

Returns an array containing all the values from array that are not present in any of the other arrays.

Example

A runnable snippet:

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2); print_r($result);

Output:

Array ( [b] => brown [c] => blue [0] => red )

Changelog

VersionChange
8.0.0This function can now be called with only one parameter. Formerly, at least two parameters have been required.

See also

array_diff(), array_diff_uassoc(), array_udiff_assoc(), array_udiff_uassoc(), array_intersect(), array_intersect_assoc()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page array_diff_assoc() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.