PHP array_uintersect_uassoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_uintersect_uassoc() returns the entries of the first array that exist in all other arrays, using one callback to compare values and another to compare keys.

This gives you complete control over what counts as a match, for instance ignoring case in both the keys and the values.

Syntax

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

PHP
$common = array_uintersect_uassoc($a, $b, 'strcasecmp', 'strcasecmp');
CallWhat it does
array_uintersect_uassoc($a, $b, 'strcasecmp', 'strcasecmp')Ignores case in both keys and values.
array_uintersect_uassoc($a, $b, 'strcasecmp', 'strcmp')Ignores case in values only.
array_uintersect_uassoc($a, $b, $valueCmp, $keyCmp)Uses your own callbacks.

The value callback comes first and the key callback comes last. Both go after all the arrays.

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

Parameters

ParameterDescription
arrayRequired. The first array.
arraysRequired. One or more arrays to compare with.
value_compare_funcRequired. Callback to compare two values. Must return an integer.
key_compare_funcRequired. Callback to compare two keys. Must return an integer.

Return Value

Return ValueDescription
arrayThe entries of the first array that match (by key and value) in all the other arrays. Keys are preserved.

PHP Examples

Example 1 — Ignore Case Everywhere

Match entries whose keys and values are equal ignoring case.

PHP
<?php
$a = ["A" => "red", "B" => "green"];
$b = ["a" => "RED", "b" => "blue"];

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

echo json_encode($common), PHP_EOL;

How It Works

  1. The first callback compares values and the second compares keys.
  2. Key "A" matches "a" and "red" matches "RED", so this entry is kept.
  3. Key "B" matches "b", but "green" is not "blue", so it is dropped.
  4. The result uses the key and value from the first array.

Example 2 — Strict or Loose Keys

Changing only the key callback changes the result.

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

echo json_encode(array_uintersect_uassoc($a, $b, $value, fn($x, $y) => strcmp($x, $y))), PHP_EOL;
echo json_encode(array_uintersect_uassoc($a, $b, $value, fn($x, $y) => strcasecmp($x, $y))), PHP_EOL;

How It Works

  1. The values are equal, but the keys differ only by case.
  2. With strcmp() for keys, "A" and "a" do not match, so the result is empty.
  3. With strcasecmp() for keys, they match and the entry is kept.
  4. The empty result prints as [] with json_encode().

Example 3 — Match Permissions

Find which required permissions a user already has.

PHP
<?php
$required = ["Admin" => "read", "Editor" => "write"];
$user = ["admin" => "READ", "editor" => "delete"];

$match = array_uintersect_uassoc(
    $required,
    $user,
    fn($x, $y) => strcasecmp($x, $y),
    fn($x, $y) => strcasecmp($x, $y)
);

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

How It Works

  1. The required list and the user list use different capitalization.
  2. Both callbacks ignore case, so "Admin" matches "admin" and "read" matches "READ".
  3. "Editor" matches "editor", but "write" is not "delete".
  4. Only the Admin entry matches, and array_keys() prints it.

Frequently Asked Questions

It returns entries of the first array that exist in all other arrays, using a callback for values and another for keys.
The value callback is first and the key callback is last.
array_uintersect_assoc() compares keys in a fixed way. array_uintersect_uassoc() lets you supply a custom key comparison.
The keys and values from the first array.
Yes. Names like 'strcmp' and 'strcasecmp' work well, and so do arrow functions.

Next: array_unique()

array_unique() removes duplicate values from an array.

array_unique() 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