array_diff_uassoc(): Difference between two arrays considering additional key checks through callback function

This function takes two or more arrays as input and then returns the result array as difference.
$new_array = array_diff_uassoc ($array1, $array2,....,my_function());
my_function() : is the callback function to return integer less than , equal to or greater than zero after comparing the keys.

Returns all elements of $array1 which are not present in $array2 considering keys also. Keys comparison is done through call-back function my_function().

Example with two arrays

function my_function($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}

$first=array('One' =>'First','Two'=>'Second','Three'=>'Third','Fourth');
$second=array('One'=> 'First','Fourth','2nd');

$result=array_diff_uassoc($first,$second,"my_function");
while (list ($key, $val) = each ($result)) {
echo "$key -> $val <br>";
}
Output is here.
Two -> Second 
Three -> Third 
Keys of the first index are retained. ( no re-indexing done here) . The element with value Third is included in output as the index or key is not matching with $second array though the value is matching.

Example 1: Basic Usage of array_diff_uassoc()

This example compares two associative arrays based on both keys and values using a custom function for key comparison.

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

function custom_compare($key1, $key2) {
    return strcmp($key1, $key2); // Case-sensitive comparison
}

$result = array_diff_uassoc($array1, $array2, 'custom_compare');
print_r($result);
Output:
Array
(
    [b] => banana
    [c] => cherry
)
---

Example 2: Discarding Items Conditionally

This example demonstrates removing items from the first array that have the same key and value as in the second array, with case-insensitive comparison.

More on string function strcasecmp()
$array1 = ['A' => 'apple', 'b' => 'banana', 'C' => 'cherry'];
$array2 = ['a' => 'apple', 'B' => 'banana', 'd' => 'date'];

function case_insensitive_compare($key1, $key2) {
    return strcasecmp($key1, $key2); // Case-insensitive comparison
}

$result = array_diff_uassoc($array1, $array2, 'case_insensitive_compare');
print_r($result);
Output:
Array
(
    [C] => cherry
)
For case sensitive compare use strcmp()
$array1 = ['A' => 'apple', 'b' => 'banana', 'C' => 'cherry'];
$array2 = ['a' => 'apple', 'B' => 'banana', 'd' => 'date'];

function case_insensitive_compare($key1, $key2) {
    return strcmp($key1, $key2); // Case-insensitive comparison
}

$result = array_diff_uassoc($array1, $array2, 'case_insensitive_compare');
print_r($result);
Output
Array ( [A] => apple [b] => banana [C] => cherry )

Difference between arrays considering values
Array REFERENCE
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











    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