PHP array_uintersect_assoc 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_assoc function computes the intersection of arrays with additional index check, comparing data by a callback function.
array_uintersect_assoc returns an array containing all values from array1 that are present in all arguments. Keys are used in comparison.
Syntax: array_uintersect_assoc(array $array1, array $array2, …, callable $value_compare_func): array. The callback must return an integer less than, equal to, or greater than zero.
This example shows a simple case-insensitive string comparison intersection.
basic_array_uintersect_assoc.php
<?php
declare(strict_types=1);
$array1 = [“a” => “green”, “b” => “brown”, “c” => “blue”]; $array2 = [“a” => “GREEN”, “B” => “yellow”, “c” => “BLUE”];
$result = array_uintersect_assoc($array1, $array2, “strcasecmp”);
print_r($result);
This compares arrays case-insensitively using strcasecmp. Only elements with matching keys AND case-insensitive values are included.
Compare arrays of objects using a custom callback function for intersection.
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”, 899.99), “p3” => new Product(“Tablet”, 399.99) ];
$result = array_uintersect_assoc($products1, $products2, fn($a, $b) => $a->name <=> $b->name);
print_r($result);
This intersects arrays of Product objects comparing only the name property. The same key (“p1”) and matching names result in inclusion in the output.
Compare floating-point numbers with a precision tolerance in the intersection.
float_comparison.php
<?php
declare(strict_types=1);
$array1 = [“a” => 1.2345, “b” => 2.3456, “c” => 3.4567]; $array2 = [“a” => 1.2346, “b” => 2.3450, “d” => 3.4567];
$result = array_uintersect_assoc($array1, $array2, function($a, $b) { return abs($a - $b) < 0.001 ? 0 : ($a <=> $b); });
print_r($result);
This compares floats with 0.001 precision tolerance. Keys must match exactly, while values are considered equal if within the specified tolerance.
Compute intersection across multiple arrays with custom comparison function.
multi_array_intersection.php
<?php
declare(strict_types=1);
$array1 = [“a” => “apple”, “b” => “banana”, “c” => “cherry”]; $array2 = [“a” => “APPLE”, “c” => “CHERRY”, “d” => “date”]; $array3 = [“a” => “Apple”, “c” => “Cherry”, “e” => “elderberry”];
$result = array_uintersect_assoc($array1, $array2, $array3, “strcasecmp”);
print_r($result);
This finds elements present in all three arrays with matching keys and case-insensitive matching values using strcasecmp.
Compare arrays containing complex data structures with a custom callback.
complex_comparison.php
<?php
declare(strict_types=1);
$array1 = [ “user1” => [“id” => 1, “name” => “John”], “user2” => [“id” => 2, “name” => “Jane”] ];
$array2 = [ “user1” => [“id” => 1, “name” => “JOHN”], “user3” => [“id” => 3, “name” => “Alice”] ];
$result = array_uintersect_assoc($array1, $array2, function($a, $b) { return $a[“id”] <=> $b[“id”]; });
print_r($result);
This intersects arrays of associative arrays comparing only the “id” field. The key “user1” matches and the id values are equal, so it’s included.
Consistent Callbacks: Ensure callback returns proper comparison values.
Key Awareness: Remember both keys and values are compared.
Performance: For large arrays, optimize callback functions.
Type Safety: Add type hints in callbacks for robustness.
PHP array_uintersect_assoc Documentation
This tutorial covered the PHP array_uintersect_assoc function with practical examples showing its usage for array intersection scenarios.
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.