array_uintersect_uassoc(): custom intersection of arrays with user-defined key and value comparisons

The array_uintersect_uassoc() function in PHP is used to compute the intersection of arrays, comparing both the data and keys using user-defined comparison functions. This is particularly helpful when you need fine control over how the elements and keys are compared between multiple arrays.

Syntax

array_uintersect_uassoc(array $array1, array ...$arrays, callable $value_compare_func, callable $key_compare_func): array
  • $array1: The first array to compare.
  • $arrays: One or more arrays to intersect with the first array.
  • $value_compare_func: A user-defined callback function for comparing the values of the arrays.
  • $key_compare_func: A user-defined callback function for comparing the keys of the arrays.

Example 1: Simple Intersect Using Custom Data and Key Comparison

$array1 = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
$array2 = ['a' => 'apple', 'b' => 'berry', 'c' => 'cherry'];

$result = array_uintersect_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strcmp($a, $b);
    },
    function($a, $b) {
        return strcmp($a, $b);
    }
);

print_r($result);
Output:
Array
(
    [a] => apple
    [c] => cherry
)

Example 2: Case-Insensitive Key Comparison

$array1 = ['A' => 'apple', 'B' => 'banana', 'C' => 'cherry'];
$array2 = ['a' => 'apple', 'b' => 'berry', 'c' => 'cherry'];

$result = array_uintersect_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strcmp($a, $b);
    },
    function($a, $b) {
        return strcasecmp($a, $b); // Case-insensitive key comparison
    }
);

print_r($result);
Output:
Array
(
    [A] => apple
    [C] => cherry
)

Example 3: Length-Based Key and Value Comparison

$array1 = ['apple' => 'red', 'banana' => 'yellow', 'cherry' => 'red'];
$array2 = ['apple' => 'green', 'grape' => 'purple', 'cherry' => 'dark red'];

$result = array_uintersect_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strlen($a) - strlen($b); // Compare values by length
    },
    function($a, $b) {
        return strlen($a) - strlen($b); // Compare keys by length
    }
);

print_r($result);
Output:
Array
(
    [cherry] => red
)

Conclusion

The array_uintersect_uassoc() function provides fine-grained control for intersecting arrays based on custom comparison rules for both values and keys. It is particularly useful when working with associative arrays where the comparison logic for keys and values can differ.


array_uintersect_assoc(): array intersections with custom value comparison Filling an array with value
Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer