array_uintersect_assoc(): array intersections with custom value comparison

The array_uintersect_assoc() function in PHP returns the intersection of arrays with additional index association checks. A user-defined callback function is used for value comparison. The function compares both keys and values, making it useful when both are important in your intersection logic.

Syntax

array_uintersect_assoc(array $array1, array $array2, callable $callback): array

Example 1: Simple Array Intersection with Index Comparison

function compare_values($a, $b) {
    return strcmp($a, $b);
}

$array1 = ['a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry'];
$array2 = ['a' => 'apple', 'b' => 'Banana', 'c' => 'cherry'];

$result = array_uintersect_assoc($array1, $array2, 'compare_values');
print_r($result);
Output
Array
(
    [b] => Banana
)

Example 2: Numeric Array Intersection with Custom Comparison

function num_compare($a, $b) {
    return $a <=> $b;
}

$array1 = [0 => 10, 1 => 20, 2 => 30];
$array2 = [0 => 10, 2 => 30, 3 => 40];

$result = array_uintersect_assoc($array1, $array2, 'num_compare');
print_r($result);
Output
Array
(
    [0] => 10
    [2] => 30
)

Conclusion

The array_uintersect_assoc() function is ideal for cases where both key and value need to be considered during intersection, providing more control with the custom comparison callback.
array_uintersect_uassoc(): custom intersection of arrays with user-defined key and value comparisons
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