PHP array_uintersect_uassoc function tutorial shows how to compute array intersection with custom comparison in PHP. Learn with practical examples.
last modified March 13, 2025
The PHP array_uintersect_uassoc function computes the intersection of arrays with additional index check and custom comparison functions. It’s useful for complex array comparisons.
The array_uintersect_uassoc function compares arrays using two callback functions. One for value comparison and one for key comparison.
Syntax: array_uintersect_uassoc(array $array1, array $array2, …, callable $value_compare_func, callable $key_compare_func): array. It returns an array containing all values present in all arguments.
This shows simple intersection with custom value and key comparison functions.
basic_array_uintersect_uassoc.php
<?php
declare(strict_types=1);
$array1 = [“a” => “green”, “b” => “brown”, “c” => “blue”]; $array2 = [“a” => “GREEN”, “B” => “brown”, “yellow”];
$result = array_uintersect_uassoc( $array1, $array2, fn($a, $b) => strcasecmp($a, $b), // Case-insensitive value compare fn($a, $b) => strcasecmp($a, $b) // Case-insensitive key compare );
print_r($result);
This finds intersection using case-insensitive comparison for both values and keys. Only “green”/“GREEN” with key “a” appears in both arrays.
Use custom callbacks to compare objects in arrays by their properties.
object_comparison.php
<?php
declare(strict_types=1);
class Product { public function __construct( public string $name, public float $price ) {} }
$products1 = [ “p1” => new Product(“Laptop”, 999.99), “p2” => new Product(“Phone”, 699.99) ];
$products2 = [ “P1” => new Product(“Laptop”, 999.99), “p3” => new Product(“Tablet”, 399.99) ];
$result = array_uintersect_uassoc( $products1, $products2, fn($a, $b) => $a->name <=> $b->name, // Compare by name fn($a, $b) => strcasecmp($a, $b) // Case-insensitive key compare );
print_r($result);
This finds products with same name, ignoring key case. The Laptop product is included in the result despite different key cases (“p1” vs “P1”).
Implement advanced key comparison logic for specialized intersection needs.
complex_key_comparison.php
<?php
declare(strict_types=1);
$array1 = [“user_1” => “Alice”, “user_2” => “Bob”, “admin” => “Charlie”]; $array2 = [“USER-1” => “Alice”, “guest” => “Dave”, “ADMIN” => “Charlie”];
$result = array_uintersect_uassoc( $array1, $array2, fn($a, $b) => strcmp($a, $b), // Standard value comparison function($a, $b) { // Custom key comparison $normalize = fn($k) => strtolower(str_replace([’_’, ‘-’], ‘’, $k)); return strcmp($normalize($a), $normalize($b)); } );
print_r($result);
This normalizes keys by removing underscores/dashes and ignoring case before comparison. Matches “user_1” with “USER-1” and “admin” with “ADMIN”.
Compare more than two arrays with custom comparison functions.
multiple_array_comparison.php
<?php
declare(strict_types=1);
$array1 = [“A” => 1, “B” => 2, “C” => 3]; $array2 = [“a” => 1, “b” => 4, “c” => 3]; $array3 = [“A” => 1, “B” => 5, “C” => 3];
$result = array_uintersect_uassoc( $array1, $array2, $array3, fn($a, $b) => $a <=> $b, // Numeric value comparison fn($a, $b) => strcasecmp($a, $b) // Case-insensitive key comparison );
print_r($result);
This finds entries with same values and case-insensitive matching keys across three arrays. Only keys “A”/“a” and “C”/“c” with value 1 and 3 match.
Implement complex value comparison logic for specialized intersection needs.
custom_value_comparison.php
<?php
declare(strict_types=1);
$array1 = [“id1” => [“name” => “Alice”, “score” => 85], “id2” => [“name” => “Bob”, “score” => 90]]; $array2 = [“ID1” => [“name” => “Alice”, “score” => 85], “id3” => [“name” => “Charlie”, “score” => 95]];
$result = array_uintersect_uassoc( $array1, $array2, function($a, $b) { // Compare associative arrays return $a[“name”] <=> $b[“name”] ?: $a[“score”] <=> $b[“score”]; }, fn($a, $b) => strcasecmp($a, $b) // Case-insensitive key comparison );
print_r($result);
This compares nested arrays by both name and score, while matching keys case- insensitively. Only Alice’s record matches in both arrays with same details.
Consistent Comparisons: Ensure comparison functions are transitive.
Performance: Optimize callbacks for large arrays.
Type Safety: Add type hints in callbacks for PHP 8+.
Readability: Use named functions for complex logic.
Testing: Verify edge cases in comparison functions.
PHP array_uintersect_uassoc Documentation
This tutorial covered the PHP array_uintersect_uassoc function with practical examples showing its usage for complex array intersections.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.