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');

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.

Post your comments , suggestion , error , requirements etc here .

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