PHP array_udiff() Function

The array_udiff() function compares the values of two or more, and returns the differences.

Note: This function uses a user-defined function to compare the values!

Returns an array containing the entries from array1 that are not present in any of the other arrays

ParameterIs RequiredDescription
array1Required.Array to compare from
array2Required.Array to compare against
array3,…Optional.More arrays to compare against
myfunctionRequired.A string that define a callable comparison function.

Syntax

 array_udiff(array1,array2,array3...,myfunction) 

Example

function myfunctiondemo($a,$b)
{
if ($a===$b)
  {
  return 0;
  }
  return ($a>$b)?1:-1;
}

$array1=array("a"=>"Cooper","b"=>"Samuel","c"=>"Lachlan");
$array2=array("a"=>"Lachlan","b"=>"James","e"=>"Lachlan");

$result=array_udiff($array1,$array2,"myfunctiondemo");
print_r($result);

Output

Array ( [a] => Cooper [b] => Samuel ) 

Leave a Reply

Your email address will not be published. Required fields are marked *