PHP array_uintersect Function
last modified March 13, 2025
The PHP array_uintersect
function computes the intersection of
arrays using a callback function for comparison. It's useful for custom
comparison logic.
Basic Definition
The array_uintersect
function compares array values using a
callback. It returns an array containing all values present in all arguments.
Syntax: array_uintersect(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 Example
This shows simple intersection of arrays with case-insensitive comparison.
<?php declare(strict_types=1); $array1 = ["Apple", "Banana", "Cherry"]; $array2 = ["apple", "banana", "grape"]; $result = array_uintersect($array1, $array2, 'strcasecmp'); print_r($result);
This finds common elements ignoring case. The strcasecmp
callback
performs case-insensitive comparison. Both "Apple" and "Banana" match.
Custom Object Comparison
Compare arrays of objects using a custom property for intersection.
<?php declare(strict_types=1); class Product { public function __construct( public string $name, public float $price ) {} } $products1 = [ new Product("Laptop", 999.99), new Product("Phone", 699.99) ]; $products2 = [ new Product("Laptop", 899.99), new Product("Tablet", 399.99) ]; $result = array_uintersect($products1, $products2, fn($a, $b) => strcmp($a->name, $b->name)); print_r($result);
This finds products with matching names, ignoring price differences. The
callback compares the name of each Product object.
Multiple Array Intersection
Find common elements across three arrays with custom comparison.
<?php declare(strict_types=1); $array1 = [10, 20, 30, 40]; $array2 = [15, 20, 25, 30]; $array3 = [20, 30, 35, 40]; $result = array_uintersect($array1, $array2, $array3, fn($a, $b) => $a <=> $b); print_r($result);
This finds numbers present in all three arrays. The spaceship operator callback performs standard numeric comparison. Only 20 and 30 match.
Complex Value Comparison
Use complex logic to compare array elements for intersection.
<?php declare(strict_types=1); function compareLength($a, $b): int { return strlen($a) <=> strlen($b); } $array1 = ["apple", "banana", "cherry"]; $array2 = ["pear", "kiwi", "orange"]; $result = array_uintersect($array1, $array2, 'compareLength'); print_r($result);
This finds strings with matching lengths across arrays. The callback compares string lengths rather than values. "apple" (5) matches "pear" (4) +1 diff.
Case-Sensitive String Comparison
Perform exact string matching with case sensitivity.
<?php declare(strict_types=1); $array1 = ["PHP", "Python", "Java"]; $array2 = ["php", "python", "ruby"]; $result = array_uintersect($array1, $array2, 'strcmp'); print_r($result);
This shows strict case-sensitive comparison. The strcmp
callback
matches exact strings. No matches found due to case differences.
Best Practices
- Consistent Callbacks: Ensure callback returns proper comparison values.
- Type Safety: Add type hints for robust comparison logic.
- Performance: Sort arrays first for large datasets.
- Readability: Use named functions for complex comparisons.
Source
PHP array_uintersect Documentation
This tutorial covered the PHP array_uintersect
function with
practical examples showing custom array intersection scenarios.
Author
List all PHP Array Functions.