array_diff_assoc
Returns the elements of the first array missing from the others, comparing both keys and values.
Syntax
array_diff_assoc(array $array, array ...$arrays): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The input array. |
| ...$arrays | array | The arrays to compare against. |
Return value
arrayAn array of the differing key–value pairs.
Examples
Which fields changed:
<?php
$before = ['name' => 'Anna', 'city' => 'Paris', 'age' => 30];
$after = ['name' => 'Anna', 'city' => 'Berlin', 'age' => 30];
echo json_encode(array_diff_assoc($after, $before));Output:
{"city":"Berlin"}Notes
array_diff() compares values only and will not notice a value that moved to another key.
See also
- array_diff — Returns the values of the first array that are absent from the others.
- array_diff_key — Returns the elements of the first array whose keys are absent from the others.
- array_intersect — Returns the values present in every array.
Related recipes
In the tutorial
- Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.