PHP array_udiff() function

P
PHP array_sum() function
PHP array_udiff_assoc() function

The array_udiff function compares the values of an array with the values of another array or arrays and returns the difference between them.

It uses a user-defined function to compare the values. It returns an array that contains the values that are present in the first array but not in the rest of the arrays.

What is the syntax of the array_udiff function in PHP?

array_intersect_ukey(array1, array2, array3, ..., myfunction)
ParametersDetails
array1The first array to compare with other arrays – required
array2Second array to make a comparison against- required
array3, ...Further arrays to compare against – optional
user-defined functionUser-defined function to compare the keys of arrays. The function must return an integer <, = or > than 0 if the first argument is <, + or > than the second argument.
array_udiff function in PHP

Examples of the array_udiff function

Example 1. Take two arrays and return the difference using a user-defined function.

<?php
function my_function($x,$y)
{
if ($x==$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}

$array_1=array("a"=>"R","b"=>"G","c"=>"B");
$array_2=array("a"=>"B","b"=>"Bl","e"=>"B");

$result=array_udiff($a1,$a2,"my_function");
print_r($result);
?>

Example 2. Take three arrays and return the difference using a user-defined function.



<?php
function my_function($x,$y)
{
if ($x==$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}

$array_1=array("a"=>"R","b"=>"G","c"=>"B");
$array_2=array("a"=>"B","b"=>"Bl","e"=>"B");
$array_3=array("a"=>"G","b"=>"R","e"=>"Y", "f"=>"Bl");

$result=array_udiff($array_1,$array_2,$array_3,"my_function");
print_r($result);
?>

PHP array_sum() function
PHP array_udiff_assoc() function

Add comment

Tutor Network

Share this

Learn PHP from A to Z