PHP Programing language

adplus-dvertising
array_udiff() Function in PHP
Previous Home Next
adplus-dvertising

array_udiff()

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

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

In above syntax, "array1" is the array to compare from, "array2" to compare against, "array3,..." and more arrays to compare against and "myfunction" is a string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Example:

<?php
function myfunction($a,$b)
{
if ($a===$b)
   {
   return 0;
   }
   return ($a>$b)?1:-1;
}
$a1=array("a"=>"Apple","b"=>"Mango","c"=>"Orange");
$a2=array("a"=>"Orange","b"=>"Papaya","e"=>"Orange");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>

Output:

Array ( [a] => Apple [b] => Mango )

array_udiff_assoc()

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

Syntax:
array_udiff_assoc(array1,array2,array3...,myfunction)

In above syntax, "array1" is the array to compare from, "array2" to compare against, "array3,..." and more arrays to compare against and "myfunction" is a string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.

Example:

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

$a1=array("a"=>"Apple","b"=>"Orange","c"=>"Mango");
$a2=array("a"=>"Apple","b"=>"Mango","c"=>"Orange");

$result=array_udiff_assoc($a1,$a2,"myfunction");
print_r($result);
?>

Output:

Array ( [b] => Orange [c] => Mango )

array_udiff_uassoc()

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

Syntax:
array_udiff_uassoc(array1,array2,array3...,myfunction_key,myfunction_value)

In above syntax, "array1" is the array to compare from, "array2" to compare against, "array3,..." and more arrays to compare against, "myfunction_key" is the name of the user-defined function that compares the array keys and "myfunction_value" is the name of the user-defined function that compares the array values.

Example:

<?php
function myfunction_key($a,$b)
{
if ($a===$b)
   {
   return 0;
   }
   return ($a>$b)?1:-1;
}
function myfunction_value($a,$b)
{
if ($a===$b)
   {
   return 0;
   }
   return ($a>$b)?1:-1;
}
$a1=array("a"=>"Apple","b"=>"Orange","c"=>"Mango");
$a2=array("a"=>"Apple","b"=>"Orange","c"=>"Orange");

$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value");
print_r($result);
?>

Output:

Array ( [c] => Mango )

Previous Home Next