array_udiff() function in PHP


The array_udiff() function compares array values in a user-made function. It returns an array containing all the values of the first array that are not present in any of the other parameters.

Syntax

array_udiff(arr1, arr2, arr3, … , compare_func)

Parameters

  • arr1 − The first array to compare from.

  • arr2 − The second array to be compared with.

  • arr3 − More arrays to compare.

  • compare_func − The comparison function. It must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Return

The array_udiff() function returns an array containing all the values of the first array that are not present in any of the other parameters.

Example

The following is an example −

 Live Demo

<?php
function compare_func($a, $b) {
   if ($a === $b) {
      return 0;
   }
   return ($a > $b)? 1:-1;
}
$arr1 = array("a" => "laptop", "b" => "keyboard", "c" => "mouse");
$arr2 = array("a" => "laptop", "d" => "mouse");
$res = array_udiff($arr1, $arr2, "compare_func");
print_r($res);
?>

Output

Array (
   [b] => keyboard
)

Updated on: 24-Jun-2020

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements