array_uintersect_uassoc(): custom intersection of arrays with user-defined key and value comparisons

The array_uintersect_uassoc() function in PHP is used to compute the intersection of arrays, comparing both the data and keys using user-defined comparison functions. This is particularly helpful when you need fine control over how the elements and keys are compared between multiple arrays.

Syntax

array_uintersect_uassoc(array $array1, array ...$arrays, callable $value_compare_func, callable $key_compare_func): array
  • $array1: The first array to compare.
  • $arrays: One or more arrays to intersect with the first array.
  • $value_compare_func: A user-defined callback function for comparing the values of the arrays.
  • $key_compare_func: A user-defined callback function for comparing the keys of the arrays.

Example 1: Simple Intersect Using Custom Data and Key Comparison

$array1 = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
$array2 = ['a' => 'apple', 'b' => 'berry', 'c' => 'cherry'];

$result = array_uintersect_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strcmp($a, $b);
    },
    function($a, $b) {
        return strcmp($a, $b);
    }
);

print_r($result);
Output:
Array
(
    [a] => apple
    [c] => cherry
)

Example 2: Case-Insensitive Key Comparison

$array1 = ['A' => 'apple', 'B' => 'banana', 'C' => 'cherry'];
$array2 = ['a' => 'apple', 'b' => 'berry', 'c' => 'cherry'];

$result = array_uintersect_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strcmp($a, $b);
    },
    function($a, $b) {
        return strcasecmp($a, $b); // Case-insensitive key comparison
    }
);

print_r($result);
Output:
Array
(
    [A] => apple
    [C] => cherry
)

Example 3: Length-Based Key and Value Comparison

$array1 = ['apple' => 'red', 'banana' => 'yellow', 'cherry' => 'red'];
$array2 = ['apple' => 'green', 'grape' => 'purple', 'cherry' => 'dark red'];

$result = array_uintersect_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strlen($a) - strlen($b); // Compare values by length
    },
    function($a, $b) {
        return strlen($a) - strlen($b); // Compare keys by length
    }
);

print_r($result);
Output:
Array
(
    [cherry] => red
)

Conclusion

The array_uintersect_uassoc() function provides fine-grained control for intersecting arrays based on custom comparison rules for both values and keys. It is particularly useful when working with associative arrays where the comparison logic for keys and values can differ.


array_uintersect_assoc(): array intersections with custom value comparison 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