Computer >> Computer tutorials >  >> Programming >> PHP

array_udiff_uassoc() function in PHP


The array_udiff_uassoc() function compares array keys and array values in user-made functions, and returns an array. 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_uassoc(arr1, arr2, arr3, … , compare_func1, compare_func2)

Parameters

  • arr1 − The first array to compare from.

  • arr2 − The second array to be compared with.

  • arr3 − More arrays to compare.

  • compare_func1 − The comparison function that compares the array keys. 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.

  • compare_func2 − The comparison function that compares the array values. 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_uassoc() 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 −

<?php
function compare_func_key($a, $b) {
   if ($a === $b) {
      return 0;
   }
   return ($a > $b)? 1:-1;
}
function compare_func_val($a, $b) {
   if ($a === $b) {
      return 0;
   }
   return ($a > $b)? 1:-1;
}
$arr1 = array("a" => "laptop", "b" => "keyboard", "c" => "mouse");
$arr2 = array("a" => "laptop", "b" => "keyboard", "c" => "headphone");
$res = array_udiff_uassoc($arr1, $arr2, "compare_func_key”, “compare_func_val");
print_r($res);
?>

Output

Array (
   [c] => mouse
)