PHP array_intersect_uassoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_intersect_uassoc() returns the items of the first array that exist in all other arrays, comparing values normally and keys with your own function.

Both the value and the key must match. Values are compared as strings, and keys are compared by the callback you provide.

Syntax

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

PHP
$common = array_intersect_uassoc($a, $b, "strcasecmp");
CallWhat it does
array_intersect_uassoc($a, $b, "strcasecmp")Matches keys without caring about upper or lower case.
array_intersect_uassoc($a, $b, "strcmp")Matches keys exactly, like array_intersect_assoc().
array_intersect_uassoc($a, $b, $myCompare)Uses your own function to decide if two keys are equal.

The callback must return 0 when two keys are equal, a number below 0 when the first is smaller, and a number above 0 when it is larger.

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

Parameters

ParameterDescription
arrayRequired. The array to check. Its items are returned.
arraysOptional. One or more arrays to compare against.
key_compare_funcRequired. A callable that compares two keys and returns an int. It is always the last argument.

Return Value

Return ValueDescription
arrayA new array with the items of the first array whose value and key both match in all other arrays.

PHP Examples

Example 1 — Case-Insensitive Keys

Use strcasecmp so that Red and red count as the same key.

PHP
<?php
$a = ["Red" => "apple", "Green" => "leaf", "Blue" => "sky"];
$b = ["red" => "apple", "green" => "grass", "blue" => "sky"];

$result = array_intersect_uassoc($a, $b, "strcasecmp");

echo json_encode($result), PHP_EOL;

How It Works

  1. Build two arrays. The keys differ only by letter case.
  2. Pass strcasecmp. It tells PHP to treat Red and red as equal keys.
  3. Check values too. Green is dropped because leaf is not grass.
  4. Read the result. Only Red and Blue match on both key and value.

Example 2 — Exact Match vs Case-Insensitive Match

The same data can give different results depending on the callback.

PHP
<?php
$a = ["A" => 1];
$b = ["a" => 1];

echo json_encode(array_intersect_uassoc($a, $b, "strcmp")), PHP_EOL;
echo json_encode(array_intersect_uassoc($a, $b, "strcasecmp")), PHP_EOL;

How It Works

  1. Use the same arrays twice. Key A and key a hold the same value.
  2. Try strcmp. It is case-sensitive, so the keys differ and the result is empty.
  3. Try strcasecmp. It ignores case, so the keys match.
  4. Compare the outputs. Only the callback changed, yet the result is different.

Example 3 — Write Your Own Key Comparison

A custom function lets you decide exactly how keys are matched.

PHP
<?php
$compare = function ($x, $y) {
    return strtolower($x) <=> strtolower($y);
};

$user  = ["Name" => "Ana", "City" => "Pune", "Age" => 30];
$saved = ["name" => "Ana", "city" => "Delhi", "age" => "30"];

echo json_encode(array_intersect_uassoc($user, $saved, $compare)), PHP_EOL;

How It Works

  1. Create a compare function. It lowercases both keys and uses <=> to return -1, 0 or 1.
  2. Pass it as the last argument. PHP calls it whenever it needs to compare two keys.
  3. Values are compared as strings. The integer 30 and the string "30" match.
  4. Read the result. City is dropped because Pune is not Delhi.

Frequently Asked Questions

It returns the items of the first array whose value and key both exist in all other arrays. Values are compared normally, and keys are compared with your callback.
array_intersect_uassoc(array $array, array ...$arrays, callable $key_compare_func): array. The callback must be the last argument.
An integer. Return 0 if the keys are equal, a negative number if the first key is smaller, and a positive number if it is larger.
Pass "strcasecmp" as the callback. It compares two strings without caring about upper or lower case.
array_intersect_ukey() compares only keys. array_intersect_uassoc() also requires the values to match.

Next: array_intersect_ukey()

array_intersect_ukey() returns the items of the first array whose keys exist in all other .

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