array_udiff(): Difference between two arrays comparing values through callback function

This function takes two or more arrays as input and then returns the result array as difference.
$new_array = array_udiff ($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. 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','2nd','Third');

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

Example 2: Comparing Multi-dimensional Arrays

We can use array_udiff() with multi-dimensional arrays to find differences between arrays based on specific keys:

More about strcmp() : Case sensitive string comparison
function compare_arrays($a, $b) {
    return strcmp($a['name'], $b['name']);
}

$array1 = [
    ['name' => 'John', 'age' => 25],
    ['name' => 'Alice', 'age' => 30]
];
$array2 = [
    ['name' => 'John', 'age' => 27],
    ['name' => 'Bob', 'age' => 35]
];

$result = array_udiff($array1, $array2, 'compare_arrays');
print_r($result);

Example 3: Ignoring Case Sensitivity During Comparison

To compare arrays while ignoring case, use strcasecmp() in the callback:

function compare_ignore_case($a, $b) {
    return strcasecmp($a, $b);
}

$array1 = ['First', 'Second', 'third'];
$array2 = ['first', 'Second', 'Third'];

$result = array_udiff($array1, $array2, 'compare_ignore_case');
print_r($result);

Use Case: Finding Unique Objects in Arrays

By comparing object properties, we can find unique objects across arrays:

class Person {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
}

function compare_objects($a, $b) {
    return strcmp($a->name, $b->name);
}

$array1 = [new Person('John'), new Person('Alice')];
$array2 = [new Person('John'), new Person('Bob')];

$result = array_udiff($array1, $array2, 'compare_objects');
print_r($result);

Difference between arrays considering values
Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











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