PHP array_uintersect_assoc Function
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.
Basic Definition
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.
Basic array_uintersect_assoc Example
This example shows a simple case-insensitive string comparison intersection.
<?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.
Custom Object Comparison
Compare arrays of objects using a custom callback function for intersection.
<?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.
Numeric Comparison with Precision
Compare floating-point numbers with a precision tolerance in the intersection.
<?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.
Multi-array Intersection
Compute intersection across multiple arrays with custom comparison function.
<?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
.
Complex Data Structure Comparison
Compare arrays containing complex data structures with a custom callback.
<?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.
Best Practices
- 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.
Source
PHP array_uintersect_assoc Documentation
This tutorial covered the PHP array_uintersect_assoc
function
with practical examples showing its usage for array intersection scenarios.
Author
List all PHP Array Functions.