PHP array_uintersect_assoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_uintersect_assoc() returns the entries of the first array that exist in all other arrays, comparing keys exactly and values with a custom function.

An entry is kept only when the same key exists in every other array and your callback says the values are equal.

Syntax

Call array_uintersect_assoc() with the values you need. Start with this line.

PHP
$common = array_uintersect_assoc($a, $b, 'strcasecmp');
CallWhat it does
array_uintersect_assoc($a, $b, 'strcasecmp')Keys must match exactly. Values are compared ignoring case.
array_uintersect_assoc($a, $b, $compare)Finds entries that are the same in both arrays.
array_uintersect_assoc($a, $b, $c, $compare)Checks against several arrays.

This is array_intersect_assoc() with a custom value comparison. If keys also need a custom comparison, use array_uintersect_uassoc().

Library declarations
PHP
array_uintersect_assoc(array $array, array ...$arrays, callable $value_compare_func): array

Parameters

ParameterDescription
arrayRequired. The first array.
arraysRequired. One or more arrays to compare with.
value_compare_funcRequired. Callback that compares two values and returns an integer.

Return Value

Return ValueDescription
arrayThe entries of the first array whose key and value appear in all the other arrays. Keys are preserved.

PHP Examples

Example 1 — Matching Keys and Values

Keep entries that have the same key and an equal value.

PHP
<?php
$a = ["a" => "Red", "b" => "Green"];
$b = ["a" => "red", "b" => "blue"];

$common = array_uintersect_assoc($a, $b, fn($x, $y) => strcasecmp($x, $y));

echo json_encode($common), PHP_EOL;

How It Works

  1. Both arrays use the keys a and b.
  2. For key a, "Red" and "red" are equal when case is ignored, so it is kept.
  3. For key b, "Green" and "blue" are different, so it is dropped.
  4. The result keeps the first array's spelling: {"a":"Red"}.

Example 2 — Keys Must Match

The same value under a different key does not count as a match.

PHP
<?php
$a = ["a" => "red"];
$b = ["A" => "red"];
$compare = fn($x, $y) => strcasecmp($x, $y);

echo json_encode(array_uintersect_assoc($a, $b, $compare)), PHP_EOL;
echo json_encode(array_uintersect($a, $b, $compare)), PHP_EOL;

How It Works

  1. The key is "a" in the first array and "A" in the second. The value "red" is the same.
  2. array_uintersect_assoc() compares keys exactly, so there is no match and the result is empty.
  3. array_uintersect() ignores keys, so "red" is found in both arrays.
  4. Empty results print as [] and the other result keeps its key a.

Example 3 — Players With the Same Score

Find players whose score did not change between two rounds.

PHP
<?php
$round1 = ["ann" => "90", "ben" => "75", "cara" => "88"];
$round2 = ["ann" => "90", "ben" => "80", "cara" => "88"];

$same = array_uintersect_assoc($round1, $round2, fn($x, $y) => (int)$x <=> (int)$y);

echo implode(", ", array_keys($same)), PHP_EOL;

How It Works

  1. Each round stores a score for each player by name.
  2. The callback turns the scores into integers and compares them.
  3. ann and cara have the same score in both rounds, but ben changed from 75 to 80.
  4. array_keys() lists the players who stayed the same.

Frequently Asked Questions

It returns the entries of the first array that appear in every other array with the same key and an equal value, where equality of values is decided by your callback.
array_uintersect() ignores keys. array_uintersect_assoc() also requires the key to match.
No. Keys are compared exactly. Use array_uintersect_uassoc() if you need a custom key comparison.
Values and keys come from the first array.
An integer: negative, zero or positive. Zero means the values are equal.

Next: array_uintersect_uassoc()

array_uintersect_uassoc() returns the entries of the first array that exist in all other a.

array_uintersect_uassoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful