Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

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): array

Parameters

ParameterTypeDescription
$arrayarrayThe input array.
...$arraysarrayThe 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.

Thanks for reading!

Donate to the site — we work hard for you.