array_udiff_uassoc(): compare arrays based on custom rules for both data and keys

The array_udiff_uassoc() function in PHP is used to compare arrays and compute the difference using user-defined comparison functions for both data and keys. This is especially useful when you need fine-grained control over how values and keys are compared between arrays.

Syntax

array_udiff_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 compare against the first array.
  • $value_compare_func: A callback function to compare the data of the arrays.
  • $key_compare_func: A callback function to compare the keys of the arrays.

Example 1: Basic Use of array_udiff_uassoc()

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

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

print_r($result);
Output:
Array
(
    [b] => banana
)

Example 2: Comparing Arrays with Case-Insensitive Key Comparison

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

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

print_r($result);
Output:
Array
(
    [B] => banana
)

Example 3: Custom Data and Key Comparison Using Length

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

$result = array_udiff_uassoc(
    $array1,
    $array2,
    function($a, $b) {
        return strlen($a) - strlen($b);
    },
    function($a, $b) {
        return strlen($a) - strlen($b);
    }
);

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

Conclusion

The array_udiff_uassoc() function provides the flexibility to compare arrays with custom rules for both values and keys. Whether you need to perform case-insensitive comparisons or use more complex logic like length-based comparisons, this function helps you maintain fine control over the comparison process. It's especially useful for dealing with associative arrays where both data and key differences matter.


Joining Two Arrays by array_merge
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