As the name is suggesting, the array_diff_ukey function compares two or more arrays based on the keys. The keys are compared in the user-defined function.
What is the syntax of the array_diff_ukey function in PHP?
array_diff_ukey(array1, array2, array3, ..., myfunction)
Parameters | Details |
---|---|
array1 | The first array to compare with other arrays – required |
array2 | Second array to make a comparison against- required |
array3, ... | Further arrays to compare against – optional |
user-defined function | User-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. |
Examples of array_diff_ukey function
<?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_diff_ukey($a1,$a2,"myfunction");
print_r($result);
?>
In the above example, the array_diff_ukey function takes two associative arrays and compares their keys using the user-defined function. Based on the result returned by the user-defined function, an array is returned that contains the values of array 1 that are not present in array 2.
<?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"=>"black","b"=>"yellow","d"=>"brown");
$a3=array("e"=>"purple","f"=>"white","a"=>"gold");
$result=array_diff_ukey($a1,$a2,$a3,"myfunction");
print_r($result);
?>
In the above example, array_diff_ukey function compares three arrays using their keys. The keys are compared using the user-defined function. The resultant array is the difference of array_1 with array_2 and array_3.