PHP array_intersect_uassoc: How it Works with Examples

php array_intersect_uassoc

PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a custom callback function.

How the array_intersect_uassoc Works in PHP

PHP array_intersect_uassoc compares arrays and returns elements that exist in all arrays. It checks both keys and values.

The syntax looks like this:

array_intersect_uassoc(array $array1, array $array2, array ... , callable $callback)

Here is how it works:

  • $array1: the main array to compare.
  • $array2, ...: arrays to check against the main array.
  • $callback: function that compares the keys.

The function returns an array with the matched values and the matched keys.

array_intersect_uassoc checks both the value and the key. The callback handles the key comparison. If the callback says the keys match, the value comparison decides the result.

Use this function when you need both the value and the key to match. It works well for associative arrays where the order or the key matters.

For a quick example:

$result = array_intersect_uassoc($array1, $array2, 'compareFunction');

The callback defines the key comparison rule. The function returns only elements that match in both value and key.

Examples of array_intersect_uassoc in PHP

Compare Arrays with String Keys:

function compareKeys($a, $b) {
    if ($a === $b) return 0;
    return ($a > $b) ? 1 : -1;
}

$array1 = ["a" => "red", "b" => "blue", "c" => "green"];
$array2 = ["a" => "red", "b" => "yellow", "d" => "green"];

$result = array_intersect_uassoc($array1, $array2, 'compareKeys');
print_r($result);

This example compares arrays with string keys. Only the key “a” with value “red” matches. The function checks both the key and the value.

Compare Arrays with Mixed Keys:

function compareKeys($a, $b) {
    return strcmp((string)$a, (string)$b);
}

$array1 = [1 => "apple", 2 => "banana", "x" => "pear"];
$array2 = ["1" => "apple", 2 => "banana", "y" => "pear"];

$result = array_intersect_uassoc($array1, $array2, 'compareKeys');
print_r($result);

This example compares numeric and string keys. The comparison function casts keys to strings. It returns the values with keys that match after the conversion.

Here is the outout:

Array
(
[1] => apple
[2] => banana
)

Advanced Comparison with Case Sensitivity:

function compareKeys($a, $b) {
    return strcmp($a, $b);
}

$array1 = ["A" => "dog", "B" => "cat"];
$array2 = ["a" => "dog", "B" => "cat"];

$result = array_intersect_uassoc($array1, $array2, 'compareKeys');
print_r($result);

This example shows case sensitivity in key comparison. The function does not match “A” with “a”. Only the key “B” with value “cat” appears in the result.

The output:

Array
(
[B] => cat
)

Multiple Arrays with Custom Key Logic:

function compareKeys($a, $b) {
    return strcasecmp($a, $b);
}

$array1 = ["One" => 10, "Two" => 20, "Three" => 30];
$array2 = ["one" => 10, "Two" => 40, "Three" => 30];
$array3 = ["ONE" => 10, "Three" => 30, "Four" => 50];

$result = array_intersect_uassoc($array1, $array2, $array3, 'compareKeys');
print_r($result);

This example checks three arrays with case-insensitive keys. The function returns “One” with value 10 and “Three” with value 30.

The output:

Array
(
[One] => 10
[Three] => 30
)

Wrapping Up

You learned how array_intersect_uassoc works with keys and values. You also saw examples with string keys, mixed keys, case rules, and multiple arrays.

Here is a quick recap:

  • It compares values and keys across arrays.
  • It uses a custom callback for key comparison.
  • It returns only matched pairs of keys and values.

FAQs

What does PHP array_intersect_uassoc do with arrays?

array_intersect_uassoc compares two or more arrays with values and keys using a user-defined callback. It returns the matched pairs.

How do you use PHP array_intersect_uassoc with a callback?

function compare_keys($a, $b) {
    if ($a === $b) {
        return 0;
    }
    return ($a > $b) ? 1 : -1;
}

$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "yellow", "d" => "blue");

$result = array_intersect_uassoc($array1, $array2, "compare_keys");
print_r($result);
Result will show only matched key-value pairs with custom comparison.

What is the difference between array_intersect_uassoc and array_intersect_assoc?

  • array_intersect_assoc compares arrays with keys using default comparison.
  • array_intersect_uassoc compares arrays with keys using user callback.

Can PHP array_intersect_uassoc compare more than two arrays?

Yes, you can pass multiple arrays for comparison:
function cmp($a, $b) {
    if ($a === $b) return 0;
    return ($a > $b) ? 1 : -1;
}

$a1 = array("x" => "car", "y" => "bus", "z" => "train");
$a2 = array("x" => "car", "y" => "bike", "z" => "train");
$a3 = array("x" => "car", "y" => "bus", "z" => "plane");

$result = array_intersect_uassoc($a1, $a2, $a3, "cmp");
print_r($result);
The result will include keys that match in all arrays.

Similar Reads

PHP XML Parsers: Everything You Need to Know

In PHP, manipulation of XML data can be very critical when most systems' data exchange relies on the format. XML—Extensible…

PHP mail() Function: A Complete Guide

PHP mail() is a great built-in function for sending an email directly from your script for account confirmations, password resets, or sending notifications.…

PHP array_map Function: How to Transform Arrays with Examples

PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…

PHP Static Property: How It Works & Examples

PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…

How to Connect a MySQL Database to PHP?

Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

PHP filter_var: Understand How to Sanitize Input

PHP didn’t have a way to check or clean user input. Developers used scattered code—some wrote custom checks, others used…

PHP Anonymous Function: How It Works with Examples

The anonymous function in PHP lets you define a small task that doesn’t require a function name. Understand the Anonymous…

PHP $_SESSION: Secure Your Web Applications in PHP

It’s very important to remember user data for each session when building web applications. This enables a high level of…

PHP Syntax: Standard Tags, Echo, and Short Tags

Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…

PHP Integers Guide: Types, Limits, and Conversions

You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…

Previous Article

Arrow Function in JavaScript: How it Works with Examples

Next Article

How to Build Responsive HTML Email Templates

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.