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
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer