The array_udiff() function uses the user-defined function to compare the values. The function compares the values of two (or more) arrays and returns the array that contains the elements from array1 that are not present in array2 or array3, etc.
PHP array_udiff
The array_udiff() is the built-in function to distinguish between two or more arrays. The array_udiff() function compares the different values of two or more arrays using user-defined function data comparison and returns the differences.
Syntax
array_udiff(array1, array2, array3, ..., myfunction)
Parameters
The array1 parameter is required. The array to compare from.
The array2 parameter is required and an array to compare against.
The array3 parameter is Optional and has more arrays to compare against.
The myfunction is required, and a string defines the callable comparison function. The comparison function must return the integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Example
<?php function compare($a, $b) { if ($a===$b) { return 0; } return ($a > $b) ? 1 : -1; } $a1 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp"); $a2 = array("el" => "Eleven", "f" => "Finn Wolfhard", "c" => "Caleb Mclaughlin"); $op = array_udiff($a1, $a2, "compare"); print_r($op);
See the output.
➜ pro php app.php Array ( [m] => Millie Bobby Brown [n] => Noah Schnapp ) ➜ pro
The $a1 contains only one value not present in $a2.
Take four array (array1, array2, array3, and array4) and using user-defined key comparison function array_udiff().
<?php function compare($a, $b) { if ($a===$b) { return 0; } return ($a > $b) ? 1 : -1; } $a1 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp"); $a2 = array("el" => "Eleven", "f" => "Finn Wolfhard", "c" => "Caleb Mclaughlin"); $a3 = array("d" => "David Harbour", "w" => "Winona Ryder", "j" => "Joe Keary"); $a4 = array("j" => "Eleven", "f" => "Finn Wolfhard", "g" => "Gaten Matarazzo"); $op = array_udiff($a1, $a2, $a3, $a4, "compare"); print_r($op);
See the output.
➜ pro php app.php Array ( [m] => Millie Bobby Brown [n] => Noah Schnapp ) ➜ pro
Taking some arrays
Let’s take two arrays whose values are the same.
<?php function compare($a, $b) { if ($a===$b) { return 0; } return ($a > $b) ? 1 : -1; } $a1 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp"); $a2 = array("e" => "Eleven", "m" => "Millie Bobby Brown", "n" => "Noah Schnapp"); $op = array_udiff($a1, $a2, "compare"); print_r($op);
See the output.
➜ pro php app.php Array ( ) ➜ pro
Custom Callback Function
You can use the array_udiff() function to define your comparison callback. Again, I assume that both arrays have precisely the same structure.
The callback function must return the negative integer if the first argument is less than the second argument, the positive number if it’s bigger, or 0 if it’s equal.
Then, you can return any number different from 0 to show that the parameters are different and 0 if they are equal.
That’s it for this tutorial.