PHP array_udiff()Functions
Examples
Compare the key values of two arrayskey values(compare key values using a user-defined function), and return the difference:
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>
Run Example »
Definition and Usage
The array_udiff() function is used to compare the key values of two (or more) arrayskey values, and return the difference.
Note:This function compares key values using a user-defined function!
This function compares the key values of two (or more) arrays and returns a difference array that includes all key values in the compared array (array1) that are not in any other parameter array (array2orarray3etc.).
Syntax
array_udiff(array1,array2,array3...,myfunction)
| Parameter | Description |
|---|---|
| array1 | Required. The first array to be compared with other arrays. |
| array2 | Required. The array to compare with the first array. |
| array3,... | Optional. Other arrays to compare with the first array. |
| myfunction | Required. A string that defines a callable comparison function. If the first argument is <, =, > the second argument, the comparison function must return an integer <, =, > 0 accordingly. |
Technical Details
| Return value: | Returns a difference array that includes all key values in the compared array (array1) that are not in any other parameter array (array2orarray3etc.). |
|---|---|
| PHP Version: | 5.1.0+ |
More Examples
Example 1
Compare the key values of three arrayskey values(compare key values using a user-defined function), and return the difference:
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");
$result=array_udiff($a1,$a2,$a3,"myfunction");
print_r($result);
?>
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue","yellow");
$a2=array("A"=>"red","b"=>"GREEN","yellow","black");
$a3=array("a"=>"green","b"=>"red","yellow","black");
$result=array_udiff($a1,$a2,$a3,"myfunction");
print_r($result);
?>
Run Example »
Complete PHP Array Reference Other extensions