PHP array_udiff() Function
In this chapter you will learn:
- Definition for PHP array_udiff() Function
- Syntax for PHP array_udiff() Function
- Parameter for PHP array_udiff() Function
- Example - Compares the values of two arrays using a user-defined function, and returns the differences
- Example - Compares the values of three arrays using a user-defined function, and returns the differences
Definition
The array_udiff() function compares the values of two or more arrays using a user-defined function, and returns the differences.
Syntax
PHP array_udiff() Function has the following syntax.
array_udiff(array1,array2,array3...,myfunction)
Parameter
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | Array to compare from |
array2 | Required. | Array to compare against |
array3,... | Optional. | More arrays to compare against |
myfunction | Required. | A string that define a callable comparison function. |
myfunction must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument.
Example 1
Compares the values of two arrays using a user-defined function, and returns the differences
<?php/*from java2 s . c om*/
function myfunction($a,$b){
if ($a===$b){
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"A","b"=>"HTML","c"=>"java2s.com");
$a2=array("a"=>"Apple","b"=>"CSS","e"=>"PHP");
$result=array_udiff($a1,$a2,"myfunction");
print_r($result);
?>
The code above generates the following result.
Example 2
Compares the values of three arrays using a user-defined function, and returns the differences
<?php//j av a 2 s . c om
function myfunction($a,$b){
if ($a===$b){
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"A","b"=>"HTML","c"=>"java2s.com");
$a2=array("a"=>"Apple","b"=>"CSS","e"=>"PHP");
$a3=array("a"=>"A","b"=>"HTML","X","Y");
$result=array_udiff($a1,$a2,$a3,"myfunction");
print_r($result);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_udiff_assoc() Function
- Syntax for PHP array_udiff_assoc() Function
- Parameter for PHP array_udiff_assoc() Function
- Example - use a built-in function to compare the keys and values of two or more arrays, and returns the differences
Home » PHP Tutorial » PHP Array Functions