PHP 8 reference · Array Functions

PHP array_diff_assoc() Function

Return the entries of the first array whose key and value pair isn't in any of the other arrays.

PHP 8 Since 4.3

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

Syntax

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

Parameters

ParameterDescription
array The input array.
arrays Arrays to compare against.

Return value

The entries whose key or value differs.

Examples

What changedTry it yourself
<?php
$old = ["name" => "Ada", "role" => "editor", "city" => "London"];
$new = ["name" => "Ada", "role" => "admin", "city" => "London"];
print_r(array_diff_assoc($new, $old));

Output

Array
(
    [role] => admin
)
Compared with array_diff()Try it yourself
<?php
print_r(array_diff(["a", "b"], ["b", "a"]));
print_r(array_diff_assoc(["a", "b"], ["b", "a"])); // positions differ

Output

Array
(
)
Array
(
    [0] => a
    [1] => b
)

Version history

VersionChange
PHP 4.3Added

Was this page helpful?