The array_diff_uassoc function returns the difference between two or more arrays based on the keys and values. But, here the main difference lies in this function is the user-defined function implementation for the comparison of keys and values.
We will make the user_defined function concept super easy using the examples in this tutorial.
What is the syntax of the array_diff_uassoc function in PHP?
array_diff_uassoc(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 |
callback function | The function which performs the comparison among the keys and the value. |
Examples of array_diff_uassoc
<?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"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_diff_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
?>
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","f"=>"green","g"=>"blue");
$a3=array("h"=>"red","b"=>"green","g"=>"blue");
$result=array_diff_assoc($a1,$a2,$a3);
print_r($result);
?>
In the above examples, the array_diff_assoc takes keys and values from the first array and compares them with the keys and values of other arrays based on the user-defined function.